An exception is an object that represents an
error or exceptional event that has occurred. These events are
usually errors that occur because the run-time environment has detected
an operation that is impossible to carry out. Exception
objects are all children of the Throwable
class.
Exceptions represent normal error events that can occur in your
program. Examples:
IndexOutOfBoundsException
FileNotFoundException
NoSuchMethodException
Exceptions generally come in two flavors:
FileNotFoundException
) you have to
handle these (either catch them or declare that your
method can throw them).
IndexOutOfBoundsException
). You do not
need to handle these.
There is a class of exceptions called errors these
are usually not recoverable (example -
VirtualMachineError
). These exceptions do not need to
be handled.
You can also build your own exception types. These should be derived from class Exception, or from one of its subclasses.
Exceptions are used to build robust programs. Exceptions allow the programmer to recover from an error or exceptional event. Java was originally a language for embedded systems (TVs, phones, watches, etc.) These systems should never stop working, exceptions are needed for these systems.
Try this example. When
run, it crashes with an exception that arises from division by zero
Now try this revised version
of the example. When run, it does not crash when the exception is
raised. Instead, the exception is handled.
Usually, if an exception is not handled, it can cause the program to terminate unnaturally and prematurely.
The process involves:
public void myMethod() throws IOException public void yourMethod() throws IOException, AWTException, BobException
throw new BadHairDayException(); MyException m = new MyException(); throw m; if (personOnPhone != bubba) throw new Exception("Stranger on the phone!!");Notice that this is different than the keyword throws, which is used in claiming exceptions.
Any group of statements that can throw and exception, or a group of
statements that you want to watch for Runtime or Error exceptions, must be
within a try
block. At the end of
the try
block there must be either a
catch
or a finally
block.
A catch
block has a parameter that is
the type of exception this catch
block will handle.
There can be several catch
blocks for a
try
block. If an exception is thrown then the first
catch block that that has a parameter matching the exception's type
will be the one that catches the exception.
A finally
block is
ALWAYS executed no matter how control leaves a
try
block. This will happen even if a return
statement is executed in the try
block, and even if
control passes to a catch
block.
Example
try { lots of IO code opening and reading from/to files } catch (FileNotFoundException) { tell the user and probably repeat try block } catch (EOFException) { hit the End Of File marker early } catch (IOException) { blanket catch for all other IO problems } finally { make sure to close any files that might be open }
If your method does not catch a checked exception and does not declare that your method can throw it then the compiler will complain. If your method throws an exception, then the method that called your method must handle the exception or declare that it can throw that exception. If no method handles the exception then the program crashes and a message is printed out describing the exception.
The same happens if an unchecked exception should occur.
The only difference between a checked an unchecked exception is
that checked exceptions must be handled
Writing code to handle exceptions is tedious and often you have no idea what to do for error recovery. It is sometimes easier just to re-throw the checked exception as an unchecked exception.
Example
catch (Exception e) { throw new RuntimeException(e); }