Lecture 6: Exception Handling

Computer Programming

J Mwaura

Error Handling

Traditional Approaches

  • Let Run-Time Environment Abort the Program
  • Ask Run-Time Environment to Abort the Program
  • Use Error Checking
  • Using Function Return Value for Error Checking

Error Handling

Problems with Traditional Approaches

  • The 1st approach - is the worst, no warning in the program
  • The 2nd approach - is better, program aborts, but user gets notified
  • The 3rd approach - is better than the first two because, the pair that would cause the program to abort is ignored, and the program continues with the rest of the data
  • The 4th approach - is the best, but it cannot be applied in all cases.

Exception Handling Approach

In the exception handling approach, the run-time error is detected, but the program handles the error and aborts the program only if necessary

The code to detect and handle errors has a standard pattern that must be followed, and every C++ programmer should know how to handle it

Try-Catch Block

Exception Handling Approach

The exception handling is built upon 3 keywords: try, catch, and throw

  • throw - A program throws an exception when a problem shows up
  • catch - A program catches an exception with an exception handler at the place in a program where you want to handle the problem
  • try - A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks

C++ Exception Handling

Exception is an issue that arises during the execution of any program

  • C++ exception is the response to an occurence of an exceptional circumstance while the program is running, e.g., an attempt integers to divide by zero
  • Exceptions provide the way to transfer the control from one part of the program to another

Try-Catch Block

The try clause detects the possibility of error and throws an exception object

The catch clause handles the exception to prevent abortion

The two clauses must be one after the other without any code in between; they belong to the same block

Three Patterns

There are three patterns commonly used with exception handling approaches

  • 1st pattern - Try-catch block in the calling function
  • 2nd pattern - Try-catch block is in main, but the exception is thrown in another function
  • 3rd pattern - Try-catch block in both the calling and called functions

First Patterns

The try-catch block is completely contained in one function

If an exception is thrown, the rest of the try clause after the throw statement is ignored and control moves to the catch clause

The function continues after the catch clause unless the program is aborted in the catch clause

Second Patterns

The try-catch block is still in main, but the exception is thrown in another function that is called in the try clause. The throw statement in this case is in the called function

When an exception is thrown, the rest of the code in the called function is ignored and the program flow moves to the catch block in the calling function

Third Patterns

We have only one actual throw statement, which is enclosed in its own try block

The second throw statement simply rethrows the previous one

Exception Handling

End of Lecture 6

Computer Programming

That's it!

Queries about this Lesson, please send them to: jmwaura@jkuat.ac.ke

*References*

  • C++ Programming - An Object-Oriented Approach, 2019 Behrouz, Richard et.al
  • Accelerated C++ - Practical Programming by Example, 2000 Andrew, Barbara et.al
Courtesy of
Computer Programming