0% found this document useful (0 votes)
2 views

Exception Handling

The document discusses exception handling in programming, particularly in C++, outlining the types of exceptions and techniques for handling them, such as terminating the program, recovering from the exception, or logging the error. It explains the use of try/catch blocks to manage exceptions and the hierarchy of built-in exception classes in C++. Additionally, it covers the creation of custom exception classes and the process of rethrowing exceptions.

Uploaded by

wahabehsan555
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exception Handling

The document discusses exception handling in programming, particularly in C++, outlining the types of exceptions and techniques for handling them, such as terminating the program, recovering from the exception, or logging the error. It explains the use of try/catch blocks to manage exceptions and the hierarchy of built-in exception classes in C++. Additionally, it covers the creation of custom exception classes and the process of rethrowing exceptions.

Uploaded by

wahabehsan555
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Exception Handling

Department of Computer Science,


The University of Lahore, Pakistan
Exceptions
• Exception: undesirable event detectable during
program execution

• Can add exception-handling code at point


where an error can occur
Exception Handling Techniques
• When an exception occurs, the programmer
usually has three choices:
– Terminate the program

– Include code to recover from the exception

– Log the error and continue


Terminate the Program
• In some cases, it is best to let the program
terminate when an exception occurs
• For example, if the input file does not exist
when the program executes
– There is no point in continuing with the program

• The program can output an appropriate error


message and terminate
Fix the Error and Continue
• In some cases, you will want to handle the
exception and let the program continue
• For example, if a user inputs a letter in place of
a number
– The input stream will enter the fail state

• You can include the necessary code to keep


prompting the user to input a number until the
entry is valid
Log the Error and Continue
• For example, if your program is designed to run
a nuclear reactor or continuously monitor a
satellite
– It cannot be terminated if an exception occurs

• When an exception occurs


– The program should write the exception into a file
and continue to run
C++ Mechanisms of Exception Handling
• The try/catch block handles exceptions

• Exception must be thrown in a try block and


caught by a catch block

• C++ provides support to handle exceptions via


a hierarchy of classes
try/catch Block
• Statements that may generate an exception are
placed in a try block

• The try block also contains statements that


should not be executed if an exception occurs

• The try block is followed by one or more


catch blocks
try/catch Block (continued)
• The catch block:
– Specifies the type of exception it can catch
– Contains an exception handler

• If the heading of a catch block contains ...


(ellipses) in place of parameters
– Catch block can catch exceptions of all types
General syntax of the try/catch block
Using try/catch Blocks in a Program
try/catch Block (continued)
• If no exception is thrown in a try block
All catch blocks for that try block are ignored
Execution resumes after the last catch block

• If an exception is thrown in a try block


Remaining statements in that try block are ignored
try/catch Block (continued)
• The program searches catch blocks in order, looking
for an appropriate exception handler

• If the type of thrown exception matches the


parameter type in one of the catch blocks:
– Code of that catch block executes

– Remaining catch blocks are ignored


try/catch Block (continued)
Throwing an Exception
• For try/catch to work, the exception must
be thrown in the try block
• General syntax to throw an exception is:

where expression is a constant value, variable, or


object
Throwing an Exception (continued)
• The object being thrown can be:
– Specific object
– Anonymous object
• In C++
– An exception is a value
– throw is a reserved word
Throwing an Exception (continued)
Order of catch Blocks
• Catch block can catch
– All exceptions of a specific type
– All types of exceptions
• A catch block with an ellipses (three dots)
catches any type of exception
• In a sequence of try/catch blocks, if the
catch block with an ellipses is needed
– It should be the last catch block of that sequence
Example 15-7
Using C++ Exception Classes
• C++ provides support to handle exceptions via
hierarchy of classes

• The class exception is:


– The base class of the exception classes provided by
C++
Using C++ Exception Classes (continued)
• Two classes derived from exception:
– logic_error
– runtime_error

• The class invalid_argument deals with illegal


arguments used in a function call
Using C++ Exception Classes (continued)
Using C++ Exception Classes (continued)
Using C++ Exception Classes (continued)
• The class out_of_range deals with the
string subscript out_of_range error

• The class length_error handles the error if


– A length greater than the maximum allowed for a string
object is used
Using C++ Exception Classes (continued)
• If the operator new cannot allocate memory space
– It throws a bad_alloc exception

• The class runtime_error deals with errors that


occur only during program execution
• Classes overflow_error and
underflow_error
– Deal with arithmetic overflow and under-flow exceptions
– Derived from the class runtime_error
Using C++ Exception Classes (continued)
• The function what returns the string containing
exception object thrown by C++’s built-in
exception classes
exceptions out_of_range and length_error
exception bad_alloc
Creating Your Own Exception Classes
• Programmers can create exception classes to handle
exceptions not covered by C++’s exception classes and
their own exceptions
• C++ uses the same mechanism to process the
exceptions you define as for built-in exceptions
• You must throw your own exceptions using the
throw statement
• Any class can be an exception class
Creating Your Own Exception Classes
Creating Your Own Exception Classes
Creating Your Own Exception Classes
Creating Your Own Exception Classes
Creating Your Own Exception Classes
Creating Your Own Exception Classes
Creating Your Own Exception Classes
Creating Your Own Exception Classes
Creating Your Own Exception Classes
Rethrowing and Throwing an Exception
• When an exception occurs in a try block
– Control immediately passes to one of the catch
blocks
• A catch block either
– Handles the exception or partially processes the
exception and then rethrows the same exception
OR
– Rethrows another exception for the calling
environment to handle
Rethrowing and Throwing an Exception (continued)
• The general syntax to rethrow an exception caught by
a catch block is:

(in this case, the same exception is rethrown) or:

where expression is a constant value, variable, or


object
Summary
• Exception: an undesirable event detectable during
program execution
• assert checks whether an expression meets a
specified condition and terminates if not met
• try/catch block handles exceptions
• Statements that may generate an exception are placed
in a try block
• Catch block specifies the type of exception it can catch
and contains an exception handler
Summary (continued)
• If no exceptions are thrown in a try block, all catch
blocks for that try block are ignored and execution
resumes after the last catch block

• Data type of catch block parameter specifies type of


exception that catch block can catch
• Catch block can have at most one parameter
• exception is base class for exception classes
• what returns string containing the exception object
thrown by built-in exception classes
Summary (continued)
• Class exception is in the header file exception
• runtime_error handles runtime errors
• C++ enables programmers to create their own
exception classes

You might also like