Exceptions zero, that require immediate handling when encountered - - PDF document

exceptions
SMART_READER_LITE
LIVE PREVIEW

Exceptions zero, that require immediate handling when encountered - - PDF document

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


slide-1
SLIDE 1

1

Exceptions

For : COP 3330. Object oriented Programming (Using C++)

ht t p: / / www. com pgeom . com / ~pi yush/ t each/ 3330 Piyush Kumar

Material from “4th ed. Programming and Problem solving with C++, Dale, Weems.

Exceptions

  • Exceptions are run‐time anomalies, such as division by

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.

  • Useful when the code that detects the problem cannot

handle it (Exception‐Detection code). Control must be transferred to the code that can handle such error. (Exception‐Handling code).

Exceptions in C++

  • Communication between exception‐detection

and exception‐handling parts of the program in C++. It involves:

  • throw expressions
  • try blocks
  • exception classes
  • The try, throw, and catch statements

implement exception handling.

Exceptions in C++

  • If not handled properly, exceptions can cause the program

to :

  • Crash
  • Falls into unknown state
  • An exception handler is a section of program code that is

designed to execute when a particular exception occurs

  • Resolve the exception
  • Lead to known state, such as exiting the program

Standard Exceptions

Exceptions Thrown by the Language

  • new

Standard Library Routines User code, using throw statement

The throw Statement

Throw: to signal the fact that an exception has

  • ccurred; also called raise

Syntax

throw Expression

slide-2
SLIDE 2

2 The try-catch Statement

try Block catch (FormalParameter) Block catch (FormalParameter) TryCatchStatement How one part of the program catches and processes the exception that another part of the program throws. FormalParameter DataType VariableName …

Example of a try-catch Statement

try { // Statements that process personnel data and may throw // exceptions of type int, string, and SalaryError } catch ( int ) { // Statements to handle an int exception } catch ( string s ) { cout << s << endl; // Prints "Invalid customer age" // More statements to handle an age error } catch ( SalaryError ) { // Statements to handle a salary error }

Execution of try-catch

No statements throw an exception Statement following entire try-catch statement A statement throws an exception

Exception Handler

Statements to deal with exception are executed

Control moves directly to exception handler

Throwing an Exception to be Caught by the Calling Code

void Func4() { if ( error ) throw ErrType(); } Normal return void Func3() { try { Func4(); } catch ( ErrType ) { } } Function call Return from thrown exception

Practice: Dividing by ZERO

Apply what you know:

int Quotient(int numer, // The numerator int denom ) // The denominator { if (denom != 0) return numer / denom; else //What to do?? do sth. to avoid program //crash }

int Quotient(int numer, // The numerator int denom ) // The denominator { if (denom == 0) throw DivByZero(); //throw exception of class DivByZero return numer / denom; }

A Solution

slide-3
SLIDE 3

3 A Solution

/ / “ quo t i en t . cpp ”-

  • Quo

t i en t p rog ram # inc lude< ios t ream> # inc lude <s t r i ng> us ing namespace s td ; i n t Quo t i en t ( i n t , i n t ) ; c lass D i vByZero / / Excep t i

  • n

c l ass { } ; i n t ma in ( ) { i n t numer ; / / Numera t

  • r

i n t denom; / / Denomina to r cou t << "En te r numera to r and denomina to r : " ;

c i n >> numer >> denom; wh i l e ( c i n ) { t r y { cou t << "The i r quo t i en t : " << Quo t i en t (numer , denom) << end l ; } ca t ch (D ivByZero ) { cou t << " * * * Denomina tor can ' t be " << end l ; } cou t << "En te r numerat

  • r

and denomina to r : " ; c i n >> numer >> denom; } r e tu rn ; }

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 == ) t h row D ivByZero ( ) ; r e tu rn numer/ denom; }