NetBurner 3.5.6
PDF Version
Exception Try/Catch

Exception Try/Catch Application

Overview

This example program demonstrates how to use C++ exceptions in an embedded NetBurner RTOS environment. The application throws a runtime exception and catches it, displaying the error message to stdout for debugging purposes.

Application Details

Application Name:** ExceptionTryCatch

The main application performs the following operations:

  1. Initializes the system and enables diagnostics
  2. Waits for network connectivity (5 second timeout)
  3. Throws a runtime_error exception with the message "Error"
  4. Catches the exception and displays the error message
  5. Enters an infinite loop with 1-second delays

Key Features

  • Demonstrates proper C++ exception handling with try/catch blocks
  • Uses standard C++ exception types (runtime_error, exception)
  • Integrates with NetBurner RTOS system initialization
  • Outputs results to stdout for debugging via MTTTY on the debug serial port

Output

When run, the application will display:

Throwing an exception...
An exception occurred. Exception: Error

Compilation Requirements

Note
C++ exceptions are disabled by default in NetBurner applications to keep application size small. To use exceptions, you must enable them during compilation.

Required Compiler Flags

The following flags must be added to the GNU C++ compiler options:

  • -fexceptions - Enables C++ exception handling
  • -frtti - Enables Run-Time Type Information (required for exceptions)

How to Enable Exceptions

  1. Right-click on your project in the IDE
  2. Select "Properties"
  3. Navigate to "GNU C++ Compiler"
  4. Select "Miscellaneous"
  5. In the "Other Flags" field, add: -fexceptions -frtti
Warning
If these flags are not enabled, the compiler will notify you in the output console window.

Dependencies

  • NetBurner RTOS headers (init.h, nbrtos.h)
  • Standard C++ iostream library
  • Standard C++ exception handling

Usage

This example is primarily educational and demonstrates the proper setup and usage of C++ exceptions in a NetBurner embedded environment. The output can be monitored using MTTTY connected to the debug serial port.

Development Notes

  • System diagnostics are enabled in this example but should be removed for production code
  • The application waits for network connectivity before proceeding
  • Exception handling adds overhead, so consider the trade-offs for production applications