Exceptions for safe programming Why Exceptions l l

  • Slides: 8
Download presentation
Exceptions for safe programming

Exceptions for safe programming

Why Exceptions l l l exceptions are used to signal that something unusual has

Why Exceptions l l l exceptions are used to signal that something unusual has happened during program execution when signal is sent, the control is transferred to specific place in a program designated to handle this exception throwing the exception – the event of sending a signal catching the exception – handling it exception mechanism is for handling extraordinary situations: division by zero, array out of range n do not use it for ordinary transfer of control – bad style, cryptic program 2

Syntax l two constructs n try block – defines the program area where an

Syntax l two constructs n try block – defines the program area where an exception may be thrown try { // some code throw ARGUMENT; // throwing exception // more code } n l l catch block (exception handler) – processes the exception catch (TYPE PARAMETER){ // exception handling code } can be multiple catch blocks, distinguished by exception type default exception catches all un-caught exceptions catch (. . . ){ // exception handling code } 3

Operation l l when the exception is thrown, control is transferred to the first

Operation l l when the exception is thrown, control is transferred to the first catch block outside the try-block that matches the type of the thrown exception after catch-block finishes, control is passed to the first statement past the last catch-block 4

Objects as Exceptions l l classes can be used as exception types n can

Objects as Exceptions l l classes can be used as exception types n can give exception a descriptive name n can be used to pass details about exception example n empty class to give descriptive name class divide_by_zero {}; n more details to be passed class wrong. Number{. . . private: int offending. Number; } 5

Catching Outside Functions may be useful to handle the exception outside the function where

Catching Outside Functions may be useful to handle the exception outside the function where it is thrown n function is said to throw the exception l by default function may throw any exception l function prototype/head may list exceptions thrown by function, if throws anything else – special unexpected () function is invoked (by default terminates program) l syntax return. Val function. Name(parameter. List) throw (exception. Type); l l example double safe_divide(int n, int d) throw (divide_by_zero){ if (d == 0) throw divide_by_zero(); return n/double(d); } 6

Catching Standard Exceptions l l l some pre-defined functions throw standard exceptions need stdexcept

Catching Standard Exceptions l l l some pre-defined functions throw standard exceptions need stdexcept header at() – throws out_of_range new – throws bad_alloc – unsuccessful allocation, possibly out of memory example handling try{ string s=”hello”; s. at(5)=’!’; // throws out_of_range } catch(out_of_range){ cout << ”string too short!”; } 7

Questions on Exceptions l l l l what are exceptions why are they needed?

Questions on Exceptions l l l l what are exceptions why are they needed? what are try-block? catch-block? What does it mean to throw or catch an exception? what is the type of an exception? what is the default exception? how is it caught? can an object be used to throw an exception? why is that useful? how can an exception be thrown inside a function and caught outside it? what are standard exceptions? What standard exceptions have we studied? 8