Vous êtes sur la page 1sur 11

Exception Handling

Exception hierarchy

java.lang.object
java.lang.Throwable
java.lang.Exception

Java.lang.RuntimeException

Classes inheriting from Throwable allow you to throw and catch the object. If classes inheriting from Exception are thrown from a given method (or any method it calls) , you must either catch them or utilize throws in your method signature A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

RuntimeExceptions
These

are the tricky exceptions because if you forget to write a handler, you are pretty much screwed. Top 5 List
5. IllegalArgumentException 4. BufferUnderflowException 3. ArithmeticException (such as division by zero) 2. ArrayIndexOutOfBoundsException And the number one RuntimeException: 1. NullPointerException

Ways to handle an exception


1.

2.

3.

Ignore it: Exception will bubble to the top of the app and output a message indicating which exception was thrown along with a stack trace that indicates where it was thrown. Propagate the exception to another method where it can be handled (i.e. ignore it and handle it up the stack) Handle the exception where at some point using try and catch statements

Passing vs. Throwing an object


You

pass classes to an object You throw exceptions Therefore, you cannot throw an exception to a class to be handled. You can
Throw an exception that may or may not be handled by a catch farther up the stack Pass an exception to a handler class
This

exception may or may not have been previously caught

Try, Catch, Finally


try { //Statements in the try block will be //executed until an Exception is thrown //So, if one was thrown in the last line, //this never happens } catch (Exception e) { //this code is only executed if an //Exception was thrown from the try. //if you have a try, you must have a catch } finally { //this is an optional block of code that will run //in any circumstance }

Throw and Throws


Use

throw when you want to throw an exception at a given point in code Use throws in your method signature to indicate that your method has a possibility of not catching such an exception (the compiler will force you)
public void example() throws ITakeException {

throw new ITakeException(I do); }

Catch
You

can (and usually will) have multiple catch statements for any given try statement The first one listed that matches will be utilized. Therefore, always put superclasses at the end, for example:
try { throw new RuntimeException(); } catch (Exception e) { //this code will always fire } catch (RuntimeException e) { //this code will never fire bad idea }

Catches can throw exceptions!


If

your catch might generate an exception

You can ignore it and execution in your catch will cease You can put another try/catch inside the catch
In

either case, the finally after your throwing (or non-throwing) catch will execute

Making your own exceptions


In

many cases, you may want to define your own exceptions For example,
RecoverableException() might be used to sum up all types of exceptions from which the system can recover on its own UnrecoverableException() might be used to page the support guy at 3am to reboot when such exceptions occur

Errors vs. Exceptions


The

Throwable class has two derived classes:


Exception: What we discussed Error: a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Errors are an end. They do not throw exceptions.

Vous aimerez peut-être aussi