Simple code that shows how exception handling doesn't have to change your code.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 
00004 int main()
00005 {
00006     Serial  console(USBTX, USBRX);
00007 
00008     try
00009     {
00010         int z  = 0;
00011         int x = ( 1 / z );
00012         console.printf("%d", x); // Should never get here.
00013         return true;
00014     }
00015     catch(...)
00016     {
00017         // x and z are cleaned up at this point.
00018         // console was defined outside of the try.
00019         // This is a good place to place a breakpoint if you are debugging or running emulation.
00020         console.printf("Something went wrong!  Terminating immediately.\n");
00021     }
00022     
00023     return false;
00024 }