Throwable
class
Indicating the Possibility of an Exception
throws
clause in method header
void open(String fileName) throws FileNotFoundException {...}
public static int parseInt(String s) throws NumberFormatException {...}
public static void main(String [] args) throws Exception {...}
Throwing An Exception
throw new Exception();
throw new Exception("Some message");
throw new UnsupportedOperationException();
Catching Exceptions
try { ... // code potentially throwing an exception (possibly via a method invocation) } catch (exception-class-1 e1) { ... // exception handling code } catch (exception-class-1 e2) { ... // exception handling code } ... ... }catch (exception-class-n en) { ... // exception handling code }
The Exception Class Hierarchy
Throwable
Exception
Error
Exception
class
FileNotFoundException
MalformedURLException
RuntimeException
Error
class
throws
clause required
OutOfMemoryError
StackOverflowError
ClassFormatError
VirtualMachineError
RuntimeException
class
throws
clause required
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
NoSuchElementException
Iterator.next()