Exceptions
Exceptional Cases
Exceptional Cases: events that cannot be controlled.
- ie. when a file doesn’t exist
- ie. when a server goes down.
- Java exception handling facilities are used when the invocation of a method may cause something exceptional to occur.
try-throw-catch Mechanism
- Java has build in exception handling which is achieved through a system of throwing and catching exceptions.
- Java has exceptions which are predefined, however the user can define their own as well.
- Exceptions can either be caught or passed on.
- Errors are similar to Exceptions in that they also are ‘throwable’ but should not be confused with exceptions.
Typical try-throw-catch Block
try {
throw new ExceptionClass(Arguments);
} catch(Exception e) {
//ExceptionHandlingCode
}
finally
- You can append a finally block at the end of a try-catch-block.
- The finally block will always be run after a try block (whether an exception is thrown or not).
- The finally block will even be executed after a return statement within a try or catch block.
Catching Method
- Exceptions can be caught within a code block by placing code that throws exceptions in a
try
block. - The argument to the
throw
operator is always an object of some exception class. - When catching multiple exceptions, the order of the
catch
block is important.- They are examined in order and the first one that matches is executed.
- Throw more specific exceptions first; ie.
IOException
beforeException
.
Declare Method
- As an alternative to handling an exception at the block level, exceptions can be passed at the method level.
- A
throw
statement is similar to a method call, but instead of calling a method, it calls acatch
block. - Eventually every thrown exception should be caught.
User-Defined Exceptions
- Must include a constructor which passes a string argument to
super
. - Must include a no-argument constructor which passes a default string to
super
. - example:
public class BadNumberException extends Exception{...}
.
Checked Exceptions
If a method throws a checked exception then it should be handled by either:
- Using a try-catch-block:
try{}catch(Exception ex ){}
. - Including the
throws
keyword in its definition.
Predefined Checked Exception Classes
Exception | Description |
---|---|
ClassNotFoundException |
Class not found. |
CloneNotSupportedException |
Attempt to clone an object that does not implement the Cloneable interface. |
IllegalAccessException |
Access to a class is denied. |
InstantiationException |
Attempt to create an object of an abstract class or interface. |
InterruptedException |
One thread has been interrupted by another thread. |
NoSuchFieldException |
A requested field does not exist. |
NoSuchMethodException |
A requested method does not exist. |
Unchecked Exceptions (Runtime)
- Anything which extends the RuntimeException class will (should) be an unchecked exception.
- Unchecked exceptions can be caught, however not doing so will not cause a runtime or compilation error.
Predefined RuntimeExceptions
Exception | Description |
---|---|
ArithmeticException |
Arithmetic error, such as divide-by-zero. |
ArrayIndexOutOfBoundsException |
Array index is out-of-bounds. |
ArrayStoreException |
Assignment to an array element of an incompatible type. |
ClassCastException |
Invalid cast. |
IllegalArgumentException |
Illegal argument used to invoke a method. |
IllegalMonitorStateException |
Illegal monitor operation, such as waiting on an unlocked thread. |
IllegalStateException |
Environment or application is in incorrect state. |
IllegalThreadStateException |
Requested operation not compatible with current thread state. |
IndexOutOfBoundsException |
Some type of index is out-of-bounds. |
NegativeArraySizeException |
Array created with a negative size. |
NullPointerException |
Invalid use of a null reference. |
NumberFormatException |
Invalid conversion of a string to a numeric format. |
SecurityException |
Attempt to violate security. |
StringIndexOutOfBounds |
Attempt to index outside the bounds of a string. |
UnsupportedOperationException |
An unsupported operation was encountered. |