Exception Handling
Lesson 303.12 Exception Handling
Learning Objectives:
By the end of lesson, learners will be able to:
Describe Exception and Exception Handling.
Describe Java Exception Classes Hierarchy.
Demonstrate how to use Exceptions in Java with defined keywords.
Declare Exceptions in Java using defined keywords.
Create user-defined Exceptions.
Section 1 ---------------------------------
Exception Handling
Table of Contents
Topic 1: Exception Overview.
Topic 2: Exception Handling Overview.
Topic 3: Exception Categories.
Topic 4: Difference Between Error and Exception.
Topic 5: Java Exception Class Hierarchy.
Topic 6: Exception Class Methods.
Topic 7: Built-in Java Exceptions.
Topic 8: Accounting for an Exception.
Topic 9: Try-Catch Block Overview.
Topic 10: Catching Multiple Exceptions.
Topic 11: Nested Try Blocks.
Topic 12: Throws and Throw Keywords.
Topic 12a: Why we need the Throws Keyword.
Topic 13: User-defined Exception.
Introduction
In this lesson, we will identify an Exception, discuss the types of Exceptions and their classes, and demonstrate Exception Handling.
Topic 1: Exception Overview
The term Exception is used (short) for the phrase "exceptional event."
An exception is an event or abnormal condition that may happen at runtime, and disrupts the normal flow of the program. We can handle exceptions to ensure our code gets executed normally. Click here for more information about Exception.
Topic 2: Exception Handling Overview
Exception Handling in Java helps to minimize exceptions and helps to recover from exceptions. It is one of the powerful mechanisms for handling runtime and compile exceptions, allowing for the normal flow and maintenance of the application, making them bug-free.
It is an object, which is thrown at runtime.
Five keywords are used in Exception Handling: try, catch, finally, throws, and throw (note that there is a difference between throw and throws).
Exception Handling Advantages/Benefits
Separates the error handling code from normal code.
Differentiates the error types.
Maintains the normal flow of the program.
Topic 3: Exception Categories
There are three main Exception categories:
Checked (also called IOException)
Exceptions that are checked at compile-time by compiler - the programmer is prompted to handle these exceptions.
A checked exception forces you to either use a try-catch or throws keyword. All exceptions are checked exceptions except Error, Uncheck Exception (Runtime Exception), and their subclasses.
Example: IOException, ClassNotFoundException, SQLException, etc.
Unchecked (also called Runtime Exception)
Exceptions that are checked at run-time by compiler - a runtime exception happens due to a programming error.
An unchecked exception does not force you to either use try-catch or throws keyword. RuntimeException and their subclasses are unchecked exceptions. This Exception can be avoided by programmer.
Example: ArithmeticException, NullPointerException, etc.
Error: Error is irrecoverable. Some examples of errors are OutOfMemoryError, VirtualMachineError, and AssertionError.
Topic 4: Difference Between Error and Exception
Errors indicate that something severe has gone wrong; if so, the application should crash rather than try to handle the error. Errors are usually beyond the control of the programmer; therefore, programmers should not try to handle errors.
Exceptions are events that occur in the code. A programmer can handle such conditions and take necessary corrective actions. For example:
NullPointerException – When you try to use a reference that points to null.
ArithmeticException – When bad data is provided by user. For example, when you try to divide a number by zero, this exception occurs because dividing a number by zero is undefined.
ArrayIndexOutOfBoundsException – When you try to access the elements of an array out of its bounds. For example, if an array size is 5 (five elements) and you are trying to access the 10th element, you cannot because the elements are out of its bounds.
Topic 5: Java Exception Class Hierarchy
Topic 5: Java Exception Classes Hierarchy (continued) Hierarchy in Java Exception Handling:
Throwable Class –
Is the root class for the exception hierarchy in Java.
Is in the java.lang package.
Error Class –
Subclass of Throwable.
Consists of abnormal conditions that are out of one’s control and depend on the environment.
Cannot be handled, and will always result in the halting of the program.
Example: StackOverFlowError that can occur in an infinite loop or recursion.
Exception Class–
Subclass of Throwable.
Consists of abnormal conditions that can be handled explicitly.
If exceptions are handled, code will continue to execute smoothly. Click here for more information about Throwable Class.
Topic 6: Exception Class Methods
Exception class does not declare its methods; it inherits Throwable class methods.
Method Prototype | Description |
---|---|
public String getMessage() | Get a detailed message about the exception that occurred. |
public Throwable getCause() | Get the cause of the exception represented by a throwable object. |
public String toString() | Concatenates the name of the class with the result of getMessage(), and returns the resultant string. |
public void printStackTrace() | Prints the result of toString() and the contents of stack trace to the error output stream, System.err. |
public StackTraceElement [] getStackTrace() | Get each element in the stack trace in the form of an array. |
public Throwable fillInStackTrace() | Fill the stack trace with the current stack trace. |
Topic 7: Built-In Java Exceptions
Here is a list of common Java Exceptions:
Java Exception | Description |
---|---|
ArithmeticException | Arithmetic error (divide-by-zero). |
ArrayIndexOutOfBoundsException | Array index is out-of-bounds. |
IllegalArgumentException | Illegal argument used to call a method. |
IllegalThreadStateException | Requested operation not compatible with current thread state. |
IndexOutOfBoundsException | Some type of index is out-of-bounds. |
NullPointerException | Invalid use of a null reference. |
NumberFormatException | Invalid conversion of a string to a numeric format. |
StringIndexOutOfBoundsException | Attempt to index outside the bounds of a string. |
TypeNotPresentException | Type not found. |
Topic 8: Accounting for an Exception
Java’s Exception Handling consists of three operations:
Declaring Exceptions
Catching an Exception
Throwing an Exception
Topic 9: Try-Catch Block Overview
Basic Syntax: Try-Catch Block
The try block contains the code you would like to execute if an exception occurs.
The catch block cannot be used without the try block.
catch: can be followed by a finally block later.
If none of the statements in the try block generate an exception, the catch block is skipped.
The finally block is executed after the try-catch block. Otherwise, it is executed after the try block. For each try block, there can be only one finally block.
Try-Catch Block Overview (continued)
A finally block contains all of the crucial statements that must be executed whether an exception occurs or not. The statements present in this block will always execute regardless of whether exception occurs in a try block or not, such as closing a connection, a stream, etc.
The finally block is optional. It is used to execute the program whether an exception is handled or not, which means that the finally block is always executed no matter whether there is an exception or not.
Example: ArithmeticException
Create a new class named ArithmeticExceptiondemo and write the code below in that class.
In this example, we will demonstrate how to declare and initialize Exception Handling for Arithmetic numbers in our program. Note: The AritmeticException Object “e” carries information about the exception that has occurred, which can be useful in taking recovery actions. If you initialize a Positive Number to variable d, you will not get an Exception; only finally block will execute.
Output:
Example: ArrayIndexOutOfBoundsException
Create a new class named ArrayIndexOutOfBoundsExceptiondemo and write the code below in that class.
Note: The ArrayIndexOutOfBoundsException Object “e” carries information about the exception that has occurred, which can be useful in taking recovery actions.
Example: NullPointerException
In this example, we will demonstrate how to handle NullpointExeption. Create a new class named NullPointerExceptiondemo and write the code below in that class.
Note: The NullPointerException Object “e” carries information about the exception that has occurred which can be useful in taking recovery actions.
Output:
Exception Message
Each exception contains a message at the level it was thrown. This message contains what went wrong, and identifies the line where the exception occurred. To view all of the messages from every level, use printStackTrace() method:
Topic 10: Catching Multiple Exceptions
You can actually catch Exceptions at two different levels:
The Exact Exception:
For example: ArrayIndexOutOfBoundsException.
A Parent Exception:
For example: IOException.
Note: The parent of all Exceptions is called “Exception.” If you would like to catch all exceptions, this can be used. Although it makes things easier to code, it is harder to pinpoint the reason behind the exception.
Topic 10: Catching Multiple Exceptions (continued)
On the flip slide, you can make debugging a lot easier by catching different exceptions. For example:
Basic Syntax
Catching Multiple Exceptions.
Create a new class named MultipleExceptionExample and write the code below in that class. In this example, we will demonstrate how we can handle more than one exception at the same time.
Nested Try Blocks
Try/catch blocks can be nested within each other. If an Exception is not caught within the nested try block, the outer try block is able to catch it. Outer blocks can be used as another default option. For example, an inner try block will try to handle an error gracefully, but if it cannot, the outer block can catch it and terminate the program.
Throws and Throw Keywords
throws: The throws keyword is used to implicitly throw an exception, and is mainly used to throw checked exception. We can use/declare the throws keyword with only the method signature.
throw: The throw keyword is used to manually/explicitly throw an exception, and we can define our own set of conditions or rules. We can use/declare the throw keyword within the method body or any block of code. We cannot throw multiple exceptions.
Why We Need the Throws Keyword
The throws keyword is used for handling checked exceptions. By using throws, we can declare multiple exceptions at one time. The throws keyword does the same thing that try-catch does but there are some cases where you would prefer throws over try-catch. For example, should you have several methods that can cause exceptions, it would be tedious to write try-catch for each method. The code will become unnecessarily long and will be less-readable.
Example: throws Keyword
Step 1: Create a class named Example_Throw, and write the below code. We will use the throws keyword in this class with the method signature.
Step 2: Create a class named myRunner.
Why We Need the Throw Keyword
The throw keyword is used to explicitly throw a single exception. When an exception is thrown, the flow of program execution transfers from the try block to the catch block. We use the throw keyword within a method. The throw keyword can also be used for throwing custom exceptions.
Example: Throw Keyword
Step 1: Create a class named Studentinfo, and write the code below. We will use throw keyword in the class within a method.
Step 2: Create a class named StudentRunnerClass. We will define productCheck() method in this class, and we will create an instance of ProductInfo class, and then invoke the productCheck() method. If the weight value is less than 100, that method will throw an Exception (InvalidProductException).
User-Defined Exception
Java user-defined exception is an exception, and throws that exception using a keyword 'throw.' It is done by extending a class 'Exception.' In very large projects, creating a custom exception with an easily identifiable name could save coders time in several ways. Creating a custom exception is simple and can be performed this way:
After you create your custom Exception, you can throw it in the exact same way as a regular Exception.
Example 1: Creating a Custom Exception
Step 1: Create a class named MyCustomerException, and write the code below. We will declare Custom Exception in this class, and we will extend the Exception class, which means that the Exception class becomes a SuperClass.
Step 2: Create a class named MyRunnerException, and write the code below:
Example 2: Creating a Custom Exception
Step 1: Create a class named InvalidProductException, write the code below. We will declare a custom exception in this class; for that, we will extend Exception class, which means that the Exception class becomes a SuperClass.
Step 2: Create a class named ProductInfo, and write the code to the productCheck() method. We will use both the throws keyword and the throw class.
Step 3: Create a class named ProductAppRunner. We will define productCheck() method in this class, and we will create an instance of ProductInfo class, and then invoke the productCheck() method. If the weight value is less than 100, that method will throw an Exception (InvalidProductException).
throw vs. throws
throw | throws |
---|---|
Used to create a new Exception object and throw it. | Used in method definition to declare that a risky method is being called. |
Used to explicitly throw an exception. | Implicitly throw an exception and works similarly to the try-catch block. |
Can declare only one Exception at a time. | Can declare multiple Exceptions at a time. |
Example: throw new IOException (“can not open connection”); | Example: throws IOException, ArrayIndexBoundException; |
Practice Assignment
Find the assignment PA 303.12.1- Practice Assignment - Exception handling on Canvas under Assignment section.
If you have any technical questions while performing this assignment, ask the instructor for assistance.
Knowledge Checks
What is the difference between an Exception and an Error in Java?
How can you handle Exceptions in Java?
Name the different types of Exceptions in Java
Can we just use try instead of finally to catch blocks?
Can we throw an exception explicitly or manually?
In which situation will you not be able to execute the finally block?
Suppose there is a catch block in tune with a try block with three statements - #1, #2, and #3. Now, imagine that the statement is thrown in statement #2. Will there be an execution of statement #3?
Summary
An Exception is an unwanted event that interrupts the normal flow of the program. When an Exception occurs, program execution gets terminated. In such cases, we get a system-generated error message.
The good thing about Exceptions is that they can be handled in Java.
By handling the Exceptions, we can provide a meaningful message to the user about the issue rather than a system-generated message, which the user may not understand.
Five keywords are used in Exception Handling: try, catch, finally, throws, and throw (note that there is a difference between throw and throws).
References
Last updated