CSE231
Advanced Computer
Programming - Lab 6
Introduction
• An exception is an event that occurs during the execution of a program that disrupts
the normal flow of instructions.
• An exception will cause a program to halt unless it is caught and handled with special
code.
• The core advantage of exception handling is to maintain the normal flow of the
application.
• Exception is an object of class java.lang.Throwable. It’s the super class of all
exceptions in Java.
• When an error occurs within a method, the method creates an object and hands it
off to the runtime system. The object, called an exception object, contains
information about the error, including its type and the state of the program when
the error occurred.
• Creating an exception object and handing it to the runtime system is called throwing
an exception.
• After a method throws an exception, the runtime system attempts to find a method
to handle it.
Introduction (Cont.)
• The set of possible methods to handle the exception are those that had
been invoked to get to the method where the error occurred. The list of
methods is known as the call stack.
• The runtime system searches the call stack for a method that contains
a block of code that can handle the exception. This block of code is
called an exception handler.
Introduction (Cont.)
• The search begins with the method in which the error occurred
and proceeds through the call stack in the reverse order in which
the methods were called.
• When an appropriate handler is found, the runtime system
passes the exception to the handler.
• An exception handler is considered appropriate if the type of the
exception object thrown matches the type that can be handled
by the handler.
Exception Hierarchy
Exception Hierarchy
Exception Hierarchy - System Errors
Exception Hierarchy - Exceptions
Exception Hierarchy - RunTime Exceptions
Exception Hierarchy - Unchecked Exceptions
Checked and Unchecked Exceptions
Checked Exceptions:
- Exceptional situations that might arise. They are usually external errors that aren’t
related to the programmer (reading from an invalid file, network connection problems
… etc).
- Checked at compile time.
- Must be explicitly declared with throws in the functions definition if they are not
handled (catched).
Unchecked Exceptions:
- Errors: Rare JVM errors. Should never be catched.
- Runtime Errors: Programming flaws made by the developer (accessing an array out-of-
index, division by zero … etc).
- Checked at runtime. Generally should not be catched. It’s the programmer
responsibility to prevent them from happening in the first place.
Throwing Exceptions - Example 1 (Unchecked Exception)
Java File Class
Throwing Exceptions - Example 2 (Checked Exception)
Custom Example
Compilation Error
Fix
Recall the Object.clone() method
Exception Handling : Try/Catch Block
Exception will not be catched
Doesn’t implement
Cloneable
Is not printed
Exception Handling : Try/Catch Block
Doesn’t implement
Cloneable
Exception will be catched
Execution continue as normal and it’s
printed
Exception Handling : Try/Catch Block
Stack Trace
Rest of the code is
executed
Exception Handling : Try/Catch Block
Note:
- If the exception gets thrown all the way back to the main method and the main method
doesn’t catch it, the runtime system stops the program and print the exception with a “call
stack trace”.
- Deciding if and which method should handle an exception is a software design decision.
Refer to the best practices slide item number 5.
Cautions When Using Exceptions
- Exceptions handling requires more time and resources
because of instantiating a new exception object, rolling back
the call stack, and propagating the errors to the calling
methods.
- Don’t use exceptions to control the flow of the program.
- Refer to item 1 on the Java best practices slide.
Catching Multiple Exceptions / Rethrow / Finally Block
Catching Multiple Exceptions / Rethrow / Finally Block
Note - Be careful how you order the catch statements:
If the catch(RuntimeException e) statement is the first
catch instead of being the last, it will always be invoked because
RuntimeException is the super class. Remember
polymorphism!
Exception Handling : Execution Flow
Defining Custom Exception Classes
• You can define custom exception classes if the predefined classes are not
sufficient.
• Custom exceptions provide you the flexibility to add attributes and methods
that are not part of a standard Java exception.
• These can store additional information, like an application-specific error
code, or provide utility methods that can be used to handle or present the
exception to a user.
• Use the exception classes in the JAVA API whenever possible.
Defining Custom Exception Classes
To define a custom exception, you should extend one of the
descendants of the Throwable class.
1.Error: Should not be extended. It’s reserved for JVM
errors by convention.
2.Exception, or any of its children for checked exceptions
except RuntimeException.
3.RuntimeException for unchecked exception.
Defining Custom Exception Classes
A custom class that can be thrown if an array
doesn’t meet a certain criteria
Storing the array as
a data member for
easy access
The faulty array is: [1, 2, 3, 4]
Java Exceptions Best Practices
1. Use exceptions only for exceptional conditions
2. Use checked exception for recoverable conditions and
runtime exceptions for programming errors
3. Avoid unnecessary use of checked exceptions
4. Favor the use of standard exceptions
5. Throw exceptions that are appropriate to the abstraction
6. Document all exceptions thrown by each method
7. Include failure-capture information in detail message
8. Don’t ignore the exceptions
Lab Q1
Lab Q2
Lab Q3
Lab Q3
Lab Q4