Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

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

3 Exception Handling C++ exception handling is similar in concept to Java exception handling.

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

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

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

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

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

9 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; }

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

11 Catch Blocks catch(DivideByZeroException ex) { cout << “Exception occurred:” << ex.what() << ‘\n\’; } Note, the above assumes the exception is defined.

12 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 (…) { }

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

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

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

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

17 Throw Exceptions in C++ can also throw exceptions that are not objects. –C++ can throw exceptions using primitive data types.

18 Try/Catch/Throw Example Memory exception example that throws an int.

19 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;

20 Rethrowing Exceptions Example of rethrowing exceptions.

21 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) { … }

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

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

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

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

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

27 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 header file is included.

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

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

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

31 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

32 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

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

34 Defining Exception Classes Class DivideByZeroException{ public: DivideByZeroException() : message(“attempted to divide by zero”) {} const char *what() const { return message;} private: const char *message; };

35 Defining Exception Classes Once the exception class has been defined, the program simply throws the exception and catches the exception. –throws throw DivideByZeroException(); –catch catch(DivideByZeroException ex) { … }

36 Exceptions/Memory/Compilers Exception examples that demonstrate memory management on different compilers.


Download ppt "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."

Similar presentations


Ads by Google