J.E.D.I.
2 Exceptions and Assertions
2.1 Objectives
Basic exception handling has been introduced to you in your first programming course. This lesson provides a deeper understanding of exceptions and also introduces assertions
as well.
After completing this lesson, you should be able to: 1. Handle exceptions by using try, catch and finally
2. Differentiate between the use of throw and throws 3. Use existing exception classes
4. Differentiate between checked and unchecked exceptions 5. Define your own exception classes
6. Explain the benefits of using assertions 7. Use assertions
2.2 What are Exceptions?
2.2.1 Introduction
Bugs or errors in programs are very likely to occur even if written by a very skillful and organized programmer. To avoid having to spend more time on error-checking rather
than working on the actual problem itself, Java has provided us with an exception handling mechanism.
Exceptions are short for exceptional events. These are simply errors that occur during runtime, causing the normal program flow to be disrupted. There are different type of
errors that can occur. Some examples are divide by zero errors, accessing the elements of an array beyond its range, invalid input, hard disk crash, opening a non-existent file
and heap memory exhausted.
2.2.2 The Error and Exception Classes
All exceptions are subclasses, whether directly or indirectly, of the root class Throwable. Immediately under this class are the two general categories of exceptions: the Error
class and the Exception class.
The Exception class is refer to conditions that user programs can reasonably deal with. These are usually the result of some flaws in the user program code. Example of
Exceptions are the division by zero error and the array out-of-bounds error.
The Error class, on the other hand, is used by the Java run-time system to handle errors occurring in the run-time environment. These are generally beyond the control of user
programs since these are caused by the run-time environment. Examples include out of memory errors and hard disk crash.
Introduction to Programming II Page 24
J.E.D.I.
2.2.3 An Example
Consider the following program: class DivByZero {
public static void mainString args[] { System.out.println30;
System.out.println“Pls. print me.”; }
}
Running the code would display the following error message. Exception in thread main java.lang.ArithmeticException: by zero at
DivByZero.mainDivByZero.java:3 The message provides the information on the type of exception that has occurred and
the line of code where the exception had originated from. This is how the default handler deals with uncaught exceptions. When no user code handles the occurring excpetion, the
default handler goes to work. It first prints out the description of the exception that occured. Moreover, it also prints the stack trace that indicates the hierarchy of methods
where the exception happened. Lastly, the default handler causes the program to terminate.
Now, what if you want to handle the exception in a different manner? Fortunately, the Java programming language is incorporated with these three important keywords for
exception handling, the try, catch and finally keywords.
2.3 Catching Exceptions