Exception Handling • Exception: an error that occurs in during the execution of a program • Exception Handling allows a program to continue executing without terminating. • Exception Handling allows the removal of the error handling from the program logic.
Error Handling in C char filename[] = "data. txt"; FILE *fp; fp = fopen(filename, " r"); if (fp == NULL) { fprintf(stderr, "Can't open file for reading!n"); exit(1); }
Exception Handling in C++/CLI // program code // code to handle error e 1 // more program code // code to handle error e 2 … // code to be executed regardless try { // program code } most catch (Execption. Type 1 e 1) { // code to handle exception e 1 } derived classes catch (Execption. Type 2 e 2) { // code to handle exception e 2 } least or base. . . finally { // code to be executed regardless }
Exception-Throwing Functions • Stream. Reader ^sr = gcnew Stream. Reader("control. txt"); • http: //msdn. microsoft. com/enus/library/system. io. streamreader(v= VS. 90). aspx • Many methods have exceptions, e. g. , Read. Line, Read. Block