Jim Cooper
/
simple_exception
Simple code that shows how exception handling doesn't have to change your code.
main.cpp@0:734e491b2c6e, 2015-08-30 (annotated)
- Committer:
- jimcooper
- Date:
- Sun Aug 30 01:00:47 2015 +0000
- Revision:
- 0:734e491b2c6e
Simple code that shows how exception handling doesn't have to change your code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jimcooper | 0:734e491b2c6e | 1 | #include "mbed.h" |
jimcooper | 0:734e491b2c6e | 2 | |
jimcooper | 0:734e491b2c6e | 3 | |
jimcooper | 0:734e491b2c6e | 4 | int main() |
jimcooper | 0:734e491b2c6e | 5 | { |
jimcooper | 0:734e491b2c6e | 6 | Serial console(USBTX, USBRX); |
jimcooper | 0:734e491b2c6e | 7 | |
jimcooper | 0:734e491b2c6e | 8 | try |
jimcooper | 0:734e491b2c6e | 9 | { |
jimcooper | 0:734e491b2c6e | 10 | int z = 0; |
jimcooper | 0:734e491b2c6e | 11 | int x = ( 1 / z ); |
jimcooper | 0:734e491b2c6e | 12 | console.printf("%d", x); // Should never get here. |
jimcooper | 0:734e491b2c6e | 13 | return true; |
jimcooper | 0:734e491b2c6e | 14 | } |
jimcooper | 0:734e491b2c6e | 15 | catch(...) |
jimcooper | 0:734e491b2c6e | 16 | { |
jimcooper | 0:734e491b2c6e | 17 | // x and z are cleaned up at this point. |
jimcooper | 0:734e491b2c6e | 18 | // console was defined outside of the try. |
jimcooper | 0:734e491b2c6e | 19 | // This is a good place to place a breakpoint if you are debugging or running emulation. |
jimcooper | 0:734e491b2c6e | 20 | console.printf("Something went wrong! Terminating immediately.\n"); |
jimcooper | 0:734e491b2c6e | 21 | } |
jimcooper | 0:734e491b2c6e | 22 | |
jimcooper | 0:734e491b2c6e | 23 | return false; |
jimcooper | 0:734e491b2c6e | 24 | } |