Unit 5: Exceptions
A programmer must ensure the correct execution of their code,
regardless of the situation the code encounters. In other words, if a
run-time error occurs, the program should present the user with relevant
information and then quit gracefully. The built-in C++ and Java
structures used to accomplish this goal are known as exceptions. This
unit will introduce you to the concept of throwing and catching
exceptions when something goes wrong while your software is running,
explain how exceptions are used in C++ and Java, and teach you how to
handle the errors that exceptions catch. By the end of this unit, you
will be able to write programs that perform gracefully whenever run-time
errors occur.
Completing this unit should take you approximately 6 hours.
Upon successful completion of this unit, you will be able to:
- explain the roles of exceptions and exception handling in programming;
- explain how exceptions are handled in C++ and Java;
- compare exception handling in C++ to exception handling in Java; and
- apply exception handling in C++ and Java programs.
5.1 Introduction to Exceptions
Exception handling is the process of responding to the occurrence, during computation, of anomalous or unrecoverable conditions that require special processing. To prevent abnormal terminations or uncontrollable behavior, the normal flow of program execution is disrupted in order to deal with the situation cleanly. This is provided by special software and programming constructs, but also by computer hardware mechanisms like interrupts or by operating system facilities such as signals. These two articles address exceptions in a general way without relying on a specific language.
Read this article, which introduces exceptions and relates those ideas to programming languages.
This article addresses the systematic incorporation of exception handling into component-based systems. By "component-based", one can infer "object-oriented" since the use of libraries of classes, such ast STL and JCL, can be seen as the use of components, building blocks, while constructing large-scale software systems. Read this article in its entirety to get a sense of how to put exception handling to good use.
5.2: The Role of Exceptions
We will now dig deeper into the background of exceptions, which will help us learn how to use them well. In this section, the snippets of Java or C++ are general enough that you can infer behavior for most languages. Pay special attention to the discussion on calculation inaccuracy. Computers are not always capable of performing math calculations with perfect accuracy. For instance, 1/3 results in an infinitely-repeating decimal, 0.33333...... While mathematics is infinite, computers are finite. We must account for the mathematical errors computers will always produce.
An error can be a syntax error, a runtime error, or a logic error. The language compiler or interpreter can detect syntax errors. A runtime error is an unexpected event involving incorrect semantics and interrupts the execution of an instruction. A logic error results in program behavior that does not satisfy the requirements: an incorrect solution. This page describes some semantic errors that various computer languages strive to prevent and some that some languages can not detect. In the latter case, the software developer should build program features that either avoid or detect those errors.
Read this section for an explanation of how to prevent errors by writing correct programs and writing program statements to take corrective action or to prevent errors.
Runtime errors and incorrect results when executing a program are addressed by detecting them and taking corrective action when they occur, or by preventing them from occurring in the first place. Corrective action and prevention depend on an understanding of the programming process and the identification of the root cause of errors. The programming process consists of several stages: specifying the requirements for the program, designing the program, implementing the program using a programming language, testing the program, and deploying the program for operation by users.
Errors should be detected as early as possible in the programming process, and each process stage should have a verification activity: requirements, design, code, and even tests should be verified for correctness. Each stage should also be validated (that is, checked that they satisfy the requirements) to prevent errors.
Although this page uses Java syntax for its examples, the same principles apply to any language, including C++.
5.3: Exceptions in Java and C++
Now we move into the specifics of exceptions in Java and C++. While knowing how to implement exception handling in a given language, it is the general nature and purpose of exceptions that are actually more important. That is why we started with an overall discussion, accompanied by related important points.
5.3.1: Exceptions in Java
This page illustrates the Java flow to handle an exception. Java uses 'try' and 'catch' blocks for the code that handles the exception, and a 'throw' keyword used in the method where the exception occurs.
What exceptions to handle, where to place the exceptions handlers, the correspondence of 'try's to 'catches', the nesting of exception handlers, and which methods will have the throw keywords, are design decisions and are addressed in the design activity of the programming process.
Read this section, which elaborates on exceptions in Java.
This video gives a Java example of an exception handler using 'throw' and 'catch' blocks. It is a brief review of the previous discussion. An exception is a runtime error that interrupts the execution of a program statement. When an exception occurs, the runtime system transfers control to either a runtime routine that handles the exception by printing a message and then terminating the program or to a programmer-provided routine that handles the exception by executing instructions that take corrective action. This video gives a Java example of an exception handler using 'throw' and 'catch' blocks.
5.3.2: Exceptions in C++
Read this overview of the role of exceptions generated by the C++ library. This section also covers throw and try/catch concepts.
This page might seem like it duplicates some of what we have just seen, but it is valuable because it gives a different perspective on the topic. Read chapter 1 on pages 15-60.
Unit 5 Assessment
- Receive a grade
Take this assessment to see how well you understood this unit.
- This assessment does not count towards your grade. It is just for practice!
- You will see the correct answers when you submit your answers. Use this to help you study for the final exam!
- You can take this assessment as many times as you want, whenever you want.