36
Exception Handling • The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer the consequences. – Exception handling is used in situations in which the system can recover from the error causing the exception. • The recovery procedure is the exception handler.

Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Embed Size (px)

Citation preview

Page 1: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Handling

• The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer the consequences.– Exception handling is used in situations in

which the system can recover from the error causing the exception.

• The recovery procedure is the exception handler.

Page 2: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Handling

– Exception handling is designed to handle synchronous errors.

• Divide by zero, array out of bounds

– Exception handling is not intended to handle asynchronous errors.

• Disk I/O completions, networking messages, mouse clicks, etc.

– Exception handling should only be used to process exceptional situations.

Page 3: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Handling

• C++ exception handling is similar in concept to Java exception handling.

Page 4: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Handling

• A function will throw an exception when it detects an error and is unable to deal with the error.

• An exception handler processes exceptions.– There may not be an exception handler for

every exception.– If there is an exception handler, the exception

is caught and handled.

Page 5: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Handling

• A try block is used to enclose the code that may generate an error that will produce an exception.

• At least one catch block follows a try block.– A catch block specifies the type of exception

it can catch and handle.– Each catch block contains an exception

handler.

Page 6: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Handlers

• If there is no catch block for the exception, the terminate function is called. – The terminate function calls the abort

function that aborts the program.

Page 7: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Try/Catch Blocks

• If no exceptions are thrown for a particular try block, then the catch block(s) are skipped. – The program continues with the first

statement after the catch block(s).

Page 8: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Try/Catch Blocks

• If an exception is thrown for a particular try block, then the rest of the try block statements are skipped and the appropriate catch block is executed.– The program continues with the first

statement after the catch blocks.

Page 9: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Try Blocks

• Each try block begins with the keyword try. – The block is then enclosed in braces.

try{

int result = quotient(number1, number2);

cout << “The quotient is: “ << result << endl;

}

Page 10: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Catch Blocks

• Each catch block begins with the keyword catch followed by parenthesis containing an exception type and possibly a parameter name.– The block is enclosed with braces.

Page 11: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Catch Blocks

catch(DivideByZeroException ex) {

cout << “Exception occurred:” << ex.what()

<< ‘\n\’;

}

Note, the above assumes the exception is defined.

Page 12: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Catch Blocks

• A particular catch block is executed if the block’s parameter type matches the thrown object’s type.

• A catch statement followed by parenthesis enclosing an ellipsis will catch all exceptions.catch (…) {

}

Page 13: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Catch Blocks

• The type of the catch block handler parameter matches the type of the thrown object if:– They are the same type.– The catch handler parameter type is a

public base class of the class of the thrown object.

Page 14: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Catch Blocks

– The handler parameter is of a base-class pointer or reference type and the thrown object is of a derived-class pointer or reference type.

– The catch handler is of the form catch(…)

• An exact match is required.

Page 15: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Throw

• A function can specify what exceptions to throw.– When an exception occurs, control is

returned from the function and the appropriate catch block is executed.

– The point at which a function throws an exception is called the throw point.

– Once a function throws an exception, control can not be returned to that function.

Page 16: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Throw• If a function throws a const object then the

catch handler argument type must be declared const as well.

• Catch blocks can discover an error and throw an exception.

• If a function throws an exception, the function terminates, all local variables are destroyed, and control returns to the point at which that function was called.

Page 17: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Throw

• Exceptions in C++ can also throw exceptions that are not objects. – C++ can throw exceptions using primitive

data types.

Page 18: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Try/Catch/Throw Example

• Memory exception example that throws an int.

Page 19: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Rethrowing Exceptions

• If a catch block determines it is unable or does not want to handle and exception, the catch block can rethrow the exception.– The statement to rethrow an exception is:

throw;

Page 20: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Rethrowing Exceptions

• Example of rethrowing exceptions.

Page 21: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Specifications

• Any function can throw an exception. – The programmer can provide a list of specific

exceptions that a function may throw.• This list is referred to as the throw list.

returntype functionName(params) throw (typeA, typeB, typeC) {

}

Page 22: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Specifications

• If the throw() is placed after the function’s parameters list, the function will not throw any exceptions.

• A function with no exception specification can throw any exception.

Page 23: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Specifications

• If an exception not listed in the exception specification is thrown, the function unexpected is called.– The unexpected function calls the function

specified with the set_unexpected function. – If no function has been set, the unexpected

function by default calls the terminate function which calls abort.

Page 24: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exception Specifications

• If a function throws an exception of a particular class type, that function can also throw exceptions of all classes derived from that class with public inheritance.

Page 25: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Constructors and Destructors

• A thrown exception from a constructor passes to the outside world the information about the failed constructor as well as the responsibility to deal with the failure.– In order to catch the exception, the

exception handler must have access to a copy constructor for the thrown object.

Page 26: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Constructors and Destructors

• Exceptions thrown in constructors cause destructors to be called for any objects built as part of the object being constructed before the exception is thrown.– If an array of objects has been partially

constructed when an exception occurs, only the destructors for the constructed array elements will be called.

Page 27: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

new Failures

• By default, C++ throws a bad_alloc exception when a new command fails.– Not all C++ compilers can comply with this

standard and therefore, simply return 0 (zero) on a new failure.

• In this case, your program must either test the new object via a boolean test or an assertion.

• Some of these compilers can throw the exception if the <new> header file is included.

Page 28: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

new Failures

• The set_new_handler function allows one to specify a uniform method of processing every new failure.– This function takes a function pointer as it’s

parameter. • The function pointer must point to a function

that takes no arguments and returns void.

Page 29: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

new failures• The C++ standard specifies that the new

handler function should perform one of the following tasks:– Make more memory available by deleting other

dynamically allocated memory and return to the loop in operator new in an attempt to allocate memory again.

– Throw an exception of type bad_alloc.– Call function abort or exit to terminate the

program.

Page 30: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Standard Library Exceptions

• The base class for all exceptions is exception.– This class contains the function what() that

returns an error message.– Each derived class overrides what() to

provide an appropriate error message.

Page 31: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Standard Library Exceptions

• The runtime_error class is derived from exception and is the base class for errors that can only be detected at execution time.– A good program will associate each type of

execution time error with an appropriately named exception object.

• This may require defining your own exception classes.

• i.e. overflow_error

Page 32: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Standard Library Exceptions

• The logic_error class is also derived from exception and is the base class for errors in program logic.– i.e. invalid_argument or out_of_range

• The exception class is also the base class for exceptions thrown by the C++ language features.– i.e. bad_alloc, or bad_cast

Page 33: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Defining Exception Classes

• As a programmer you are able to define your own exception classes.– Such classes should contain the

constructor, a what function, and a message attribute.

Page 34: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Defining Exception Classes

Class DivideByZeroException{

public:

DivideByZeroException() :

message(“attempted to divide by zero”) {}

const char *what() const

{ return message;}

private:

const char *message;

};

Page 35: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Defining Exception Classes

• Once the exception class has been defined, the program simply throws the exception and catches the exception.– throws

throw DivideByZeroException();

– catchcatch(DivideByZeroException ex) {

}

Page 36: Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer

Exceptions/Memory/Compilers

• Exception examples that demonstrate memory management on different compilers.