Exceptions Exceptions are run ‐ time anomalies, such as division by Exceptions • zero, that require immediate handling when encountered by your program. The C++ language provides built ‐ in support for raising and handling exceptions. With C++ exception handling, your program can communicate unexpected events to a higher execution context that is better able to recover from such abnormal events. For : COP 3330. Useful when the code that detects the problem cannot • handle it (Exception ‐ Detection code). Control must be Object oriented Programming (Using C++) transferred to the code that can handle such error. ht t p: / / www. com pgeom . com / ~pi yush/ t each/ 3330 (Exception ‐ Handling code). Piyush Kumar Material from “4 th ed. Programming and Problem solving with C++, Dale, Weems. Exceptions in C++ Exceptions in C++ • Communication between exception ‐ detection • If not handled properly, exceptions can cause the program to : and exception ‐ handling parts of the program • Crash in C++. It involves: • Falls into unknown state • throw expressions • An exception handler is a section of program code that is • try blocks designed to execute when a particular exception occurs • exception classes • Resolve the exception • The try, throw, and catch statements • Lead to known state, such as exiting the program implement exception handling. Standard Exceptions The throw Statement Throw: to signal the fact that an exception has � Exceptions Thrown by occurred; also called raise � the Language • new Syntax � Standard Library Routines throw Expression � User code, using throw statement 1
The try-catch Statement Example of a try-catch Statement How one part of the program catches and processes try { the exception that another part of the program throws. // Statements that process personnel data and may throw TryCatchStatement // exceptions of type int, string, and SalaryError } try catch ( int ) Block { // Statements to handle an int exception catch (FormalParameter) } Block catch ( string s ) catch (FormalParameter) { cout << s << endl; // Prints "Invalid customer age" FormalParameter // More statements to handle an age error } DataType VariableName catch ( SalaryError ) { // Statements to handle a salary error … } Throwing an Exception to be Execution of try-catch Caught by the Calling Code A No statement throws statements throw void Func3() an exception { an exception Exception try Control moves { void Func4() directly to exception Handler Function { handler call Func4(); if ( error ) Normal } return throw ErrType(); Statements to deal with exception are executed catch ( ErrType ) { } } Statement Return from } thrown following entire try-catch exception statement Practice: Dividing by ZERO A Solution Apply what you know: int Quotient(int numer, // The numerator int Quotient(int numer, // The numerator int denom ) // The denominator int denom ) // The denominator { { if (denom == 0) if (denom != 0) throw DivByZero(); return numer / denom; //throw exception of class DivByZero else return numer / denom; //What to do?? do sth. to avoid program } //crash } 2
A Solution c i n >> numer >> denom; wh i l e ( c i n ) { / / “ quo t i en t . cpp ”- - Quo t i en t p rog ram t r y { # inc lude< ios t ream> cou t << "The i r quo t i en t : " # inc lude <s t r i ng> << Quo t i en t (numer , denom) << end l ; us ing namespace s td ; } i n t Quo t i en t ( i n t , i n t ) ; ca t ch (D ivByZero ) c lass D i vByZero / / Excep t i on c l ass { { } ; cou t << " * * * Denomina tor can ' t be 0 " i n t ma in ( ) << end l ; { } cou t << "En te r numerat o r and denomina to r : " ; i n t numer ; / / Numera t o r c i n >> numer >> denom; i n t denom; / / Denomina to r } cou t << "En te r numera to r and denomina to r : " ; r e tu rn 0 ; } i n t Quo t i en t ( / * i n * / i n tnumer , / / The numera to r / * i n * / i n tdenom) / / The denomina to r { i f ( denom == 0 ) t h row D ivByZero ( ) ; r e tu rn numer/ denom; } 3
Recommend
More recommend