Altera_Forum
Honored Contributor
21 years agoC++ exceptions handling
The following snippet illustrates code that should raise 2 exceptions.
Obviously only one exception throwing line will throw an exception . And indeed it does in Visual C++. Division by 0 is caught and derferencing a NULL pointer is caught. int test_exception(void) { int x = 1; int y = 0; int z; char c; char *ptr = NULL; try{ z = x/y; /* This line is commented or the next to have one source of exception */ c = *ptr; } catch(...){ printf("Caught an exception \n"); } return 0; } The same code does not behave the same way in the Altera IDE. One could do the following, which works : try{ if(y == 0) throw MyZeroDivException; else z = x/y; if(ptr == NULL) throw MyDereferenceException; else c = *ptr; } catch(MyZeroDivException) { // Do something } catch(MyDereferenceException) { // Do something } The questions are: How does NIOS handle division by 0 ? How does dereference of an incorrect address behave ? What are the cases when software can generate an exception ? If there are similar questions about exception handling please refer me to those. Thank you Anton