Exceptions - 2 - Exceptions in Java
When something unexpected happens:
- An
Exception
object is created. - The interpreted looks for, and calls a matching handler.
Java Syntax
try {
// dangerous code
// may throw XException, YException, ...
}
catch (XException e) {
// handle XExceptions
}
catch(YException e) {
// handle YExceptions
}
...
finally {
// will eventually get executed in any case
}
The first exception whose parameter type matches the exception is picked.
To throw
an exception manually you would:
try {
throw(e) // if e already exists
throw(new XExeption()) // throw a new exception
}
catch (XException e) {
// handle XExceptions
}
catch(YException e) {
// handle YExceptions
}
Catch or Declare
Java wants you to specify as part of a methods signature, which exceptions it may throw. This allows analysis at compile time.
Declared Exceptions
The syntax to declare is:
public String myMethod() throws XException {
// method body may throw XExceptions
// or call code that does
}
Caught Exceptions
public class ReadFromKeyboard {
public static void main (String args[]) {
// read one byte from stdin
int myChar = System.in.read();
// interpret that int as a char and print
System.out.println((char) myChar);
}
}
The call to System.in.read()
can create exceptions.
Two fix this we could use one of the following methods:
import java.io.IOExceptions;
public class ReadFromKeyboard {
public static void main (String args[]) throws IOException {
// read one byte from stdin
int myChar = System.in.read();
// interpret that int as a char and print
System.out.println((char) myChar);
}
}
This is declaring the exception.
import java.io.IOExceptions;
public class ReadFromKeyboard {
public static void main (String args[]) {
try{
// read one byte from stdin
int myChar = System.in.read();
// interpret that int as a char and print
System.out.println((char) myChar);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
This is catching the exception.
This can be bad practice as developers can make useless catchall exception handlers.
Throwables
Throwables allow certain types of exception to not be declared.
java.lang
defines the following classes that can be throw
n:
Error
classes is for non-recoverable situations.Exception
classes are for recoverable situations.
Unchecked Throwables
The compiler won’t complain for throwables part of the following super-classes:
Error
RuntimeException
java.util
Exceptions
There are several exceptions already part of the java.util
package:
ArithmeticException
- Arithmetical operations such as dividing by zero.
ArrayIndexOutOfBoundsException
- Trying to access an index that is negative or bigger than the size of an array.
FileNotFoundException
- Trying to access a file that doesn’t exist or is otherwise not accessible.
NullPointerException
- Reggering to memebers of a
null
object.
- Reggering to memebers of a
InputMismatchException
- Unexpected format e.g for
String
args.
This is quite useful.
- Unexpected format e.g for
StringsIndexOutOfBoundsException
- Using an index that is negative or beyond the length of the
String
object.
- Using an index that is negative or beyond the length of the
Have a look for standard exceptions before making your own.