COMP2511
Exceptions in Java
Prepared by
Dr. Ashesh Mahidadia
Exceptions in Java
v An exception is an event, which occurs during the execution of a program, that disrupts the
normal flow of the program’s instructions.
v When error occurs, an exception object is created and given to the runtime system, this is called
throwing an exception.
v The runtime system searches the call stack for a method that contains a block of code that can
handle the exception.
v The exception handler chosen is said to catch the exception.
COMP2511: Exceptions in Java 2
The call stack. Searching the call stack for
the exception handler.
Exceptions in Java
The Three Kinds of Exceptions
v Checked exception (IOException, SQLException, etc.)
v Error (VirtualMachineError, OutOfMemoryError, etc.)
v Runtime exception (ArrayIndexOutOfBoundsExceptions, ArithmeticException, etc.)
Checked vs. Unchecked Exceptions
v An exception’s type determines whether it’s checked or unchecked.
v All classes that are subclasses of RuntimeException (typically caused by defects in your
program’s code) or Error (typically ‘system’ issues) are unchecked exceptions.
v All classes that inherit from class Exception but not directly or indirectly from class
RuntimeException are considered to be checked exceptions.
COMP2511: Exceptions in Java 3
Exceptions in Java
v Good introduction on Exceptions at
https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html
v Unchecked Exceptions — The Controversy
https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
COMP2511: Exceptions in Java 4
https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html
https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
Hierarchy of Java Exceptions
COMP2511: Exceptions in Java 5
From the book “Java How to Program, Early Objects”, 11th Edition, by Paul J. Deitel; Harvey Deitel
Unchecked Exceptions
Checked Exceptions
Example
COMP2511: Exceptions in Java 6
try
catch
finally
User Defined Exceptions in Java
v We can also create user defined exceptions.
v All exceptions must be a child of Throwable.
v A checked exception need to extend the Exception class,
but not directly or indirectly from class RuntimeException.
v An unchecked exception (like a runtime exception) need to extend the
RuntimeException class.
COMP2511: Exceptions in Java 7
User Defined / Custom Checked Exception
• Normally we define a checked exception, by extending the Exception class.
COMP2511: Exceptions in Java 8
User Defined / Custom Exceptions: A Simple Example
COMP2511: Exceptions in Java 9
Exceptions in Inheritance
v If a subclass method overrides a superclass method,
a subclass’s throws clause can contain a subset of
a superclass’s throws clause.
It must not throw more exceptions!
v Exceptions are part of an API documentation and contract.
COMP2511: Exceptions in Java 10
Demo: Exceptions in Java
Demo …
COMP2511: Exceptions in Java 11
Assertions in Java
• An assertion is a statement in the Java that enables you to test your assumptions about your
program. Assertions are useful for checking:
• Preconditions, Post-conditions, and Class Invariants (DbC!)
• Internal Invariants and Control-Flow Invariants
• You should not use assertions:
• for argument checking in public methods.
• to do any work that your application requires for correct operation.
• Evaluating assertions should not result in side effects.
• The following document shows how to use assertions in Java :
https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html
Important: for backward compatibility, by default, Java disables assertion validation feature.
It needs to be explicitly enabled using the following command line argument:
• -enableassertions command line argument, or
• -ea command line argument
COMP2511: Exceptions in Java 12
https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html
Assert : Example
COMP2511: Exceptions in Java 13
Exceptions: Summary Points
v Consider your exception-handling and error-recovery strategy in the design process.
v Sometimes you can prevent an exception by validating data first.
v If an exception can be handled meaningfully in a method, the method should catch the
exception rather than declare it.
v If a subclass method overrides a superclass method, a subclass’s throws clause can contain a
subset of a superclass’s throws clause. It must not throw more exceptions!
v Programmers should handle checked exceptions.
v If unchecked exceptions are expected, you must handle them gracefully.
v Only the first matching catch is executed, so select your catching class(es) carefully.
v Exceptions are part of an API documentation and contract.
v Assertions can be used to check preconditions, post-conditions and invariants.
COMP2511: Exceptions in Java 14