Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.

Similar presentations


Presentation on theme: "CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling."— Presentation transcript:

1 CMSC 202 Computer Science II for Majors

2 CMSC 202UMBC Topics Exceptions Exception handling

3 CMSC 202UMBC Exceptions Exceptions refer to unusual conditions in the program These conditions could be errors that cause program to fail or other conditions that lead to error Exception is an indication of some problem that occurs during the program’s execution Thus they must be detected and handled

4 CMSC 202UMBC Current mechanism Set a flag / indicator for other code to detect Assert a condition  If condition is false, program fails Issue error message and exit  e.g. “File not found” followed by exit() Handle the error internally  Doesn’t work in real world applications

5 CMSC 202UMBC Current mechanism Problems with these mechanisms  Intermix program logic with error-handling code  Program becomes difficult to read, modify, maintain and debug C++ provides exception handling mechanism  Separation of error detection and handling

6 CMSC 202UMBC Exception Exception types  Synchronous errors Divide by zero Out-of-range array subscripts Unsuccessful memory allocations  Asynchronous errors Disk I/O Keyboard interrupt Exception handling in C++ is designed only for synchronous errors

7 CMSC 202UMBC Exception Handling This mechanism consists of following constructs:  try : this block specifies sequences of code for which exceptions need to be handled  catch : this block specifies sequences of code which handles exceptions that occur in the related try block  throw : this statement causes specific exceptions to be generated

8 CMSC 202UMBC Exception Handling … cont try blockthrow catch block catch 1 catch 2 Exception throws Exception Handlers No exceptions Next statement following try/catch

9 CMSC 202UMBC Exception Handling … cont try try { // code which can generate // multiple kinds of exceptions... } If exception occurs in try block, it terminates Program control is transferred to first catch handler after try block

10 CMSC 202UMBC Exception Handling … cont catch catch (OneTypeOfException& e1) { // code to handle exception } catch (AnotherTypeOfException& e2) { // code to handle exception } catch (... ) { // default handler for exceptions // not caught by other catch blocks }

11 CMSC 202UMBC Exception Handling … cont Catch block is the error handler The argument in parentheses is called exception parameter  It represents type of exception that catch block can process Appropriate catch handler is located by comparing thrown exception’s type with exception parameter

12 CMSC 202UMBC Exception Handling … cont If no exceptions occur in try block, all catch blocks are ignored  Control passes to first statement after last catch block If exception occurs in try block with no matching catch handler, function containing exception statement terminates

13 CMSC 202UMBC Example int main(void) { int numArray[10]; try { for (int i = 0; i <= 10; i++) { TrivialOperation(numArray,i); } // Catch the exception catch (char *str) { cout << "Exception: " << str << endl; } return 0; } void TrivialOperation(int *numArray, int index) { if(index > 9) throw "Out of range exception"; numArray[index] = index * 2; }

14 CMSC 202UMBC Stack unwinding Program stack holds all local variables declared in the function as it executes If exception is thrown but not caught in particular scope  Function-call stack is unwound  Attempt is made to catch exception in outer try/catch block Function in which exception is not caught terminates, control returns to statement that originally invoked that function

15 CMSC 202UMBC Example int main() { int x; try { x = FunctionA(-9); } catch (ExceptionType1& e1) { cout << "Exception: Value < 0" << endl; } catch (ExceptionType2& e2) { cout 100" << endl; } return 0; }

16 CMSC 202UMBC Example … cont int FunctionA(int val1) { int val2; val2 = FunctionB(val1); return 2*val2; } int FunctionB(int num) { if (num < 0) { throw ExceptionType1(); } else if (num > 100) { throw ExceptionType2(); } Assume that ExceptionType1() and ExceptionType1() have already been defined

17 CMSC 202UMBC Stack unwinding … cont main() => FunctionA() => FunctionB() Exception of type ExceptionType1 is thrown in FunctionB as value passed is less than 0 Exception not caught in FunctionB, hence unwind stack (i.e. terminate function) Control now passes to FunctionA Again no catch for exception in FunctionA, further unwind stack destroying all local variables on stack Control now passes to main, where exception is caught

18 CMSC 202UMBC Exercise What happens when main also doesn’t catches exception in previous example ? How to handle exceptions during memory allocation when new fails ? Exercise 13.26 page 806


Download ppt "CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling."

Similar presentations


Ads by Google