1 / 18

CMSC 202

CMSC 202. Computer Science II for Majors. Topics. Exceptions Exception handling. 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

ohio
Download Presentation

CMSC 202

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 202 Computer Science II for Majors

  2. Topics • Exceptions • Exception handling

  3. 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. 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. 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. 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. 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. Exception Handling … cont try block throw Exception Exception Handlers catch block No exceptions catch 1 throws catch 2 Next statement following try/catch

  9. 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. 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. 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. 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. 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. 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. Example int main() { int x; try { x = FunctionA(-9); } catch (ExceptionType1& e1) { cout << "Exception: Value < 0" << endl; } catch (ExceptionType2& e2) { cout << "Exception: Value > 100" << endl; } return 0; }

  16. 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. 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. 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

More Related