








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: PROGRAMMING IN JAVA; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Spring 2005;
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!









Java has a built in mechanism for error handling andtrapping errors
-^
Usually this means dealing with abnormal events or codeexecution that prevents the program from continuing– Array out of bounds accesses– Divide by zero– Null pointers– To name a few…
-^
Exceptions allow us to handle these events automaticallywhen they happen
Essentially, throwing exceptions in Java is a way ofterminating method execution early:
public class
String
{
public char
charAt(
int
index)
throws
IndexOutOfBoundsException
{
... throw new
IndexOutOfBoundsException();
... return
c;
} }
Anytime that you call a method that has been declaredas being able to throw an Exception, you can catch theException using a try/catch block:
-^
If an Exception is thrown inside of a try block, theexception that is returned is forwarded as an argumentto the catch block where the Exception can be handled
try
{ String text = “text”;System.out.println(text.charAt(10)); }^
catch
(IndexOutOfBoundsException e) { System.err.println(“Index out of bounds”);e.printStackTrace(); }
These are the subclasses of the RunTimeExceptionclass–
ArithmeticException– IndexOutOfBoundsException– NegativeArraySizeException– NullPointerException– ArrayStoreException– ClassCastException– IllegalArgumentException– SecurityException– IllegalMonitorStateException– IllegalStateException– UnsupportedOperationException
As we have seen, if a method throws an Exception thatis not a subclass of RunTimeException, we are requiredto deal with it if using this method
-^
One way is by using a try/catch block as we have seen
-^
You can also indicate that the calling method throws thesame Exception, essentially forwarding the responsibilityof catching the exception to the code that calls yourmethod
public void
myMethod()
throws
IOException
{
//calls a method that throws an IOException }
Since all Exceptions are subclasses of the Exceptionclass, you can generalize catch blocks to accept multipledifferent types of Exceptions by using a super class
try
{ String text = “text”;System.out.println(text.charAt(10));int n = Integer.parseInt(“abc”); }^
catch
(Exception e) { System.err.println(“Something bad happened”);e.printStackTrace(); }
Sometimes when you call a try/catch block, an Exceptioncould be thrown before some important code that needsto be run at the end of the try block
-^
The finally block can be used to run this code
-^
Even if an exception is thrown, the finally block willalways execute
try
{ String text = “text”;System.out.println(text.charAt(10)); }^
catch
(IndexOutOfBoundsException e) { System.err.println(“Index out of bounds”);e.printStackTrace(); }^
finally
{
//important code }
What type of information can you get from Exceptionobjects?– getCause()– getMessage()– printStackTrace()
-^
Sub classes of Exception can be much more elaborateand contain more information if desired
-^
You can also define your own Exception class whichmust be a subclass of Exception or one of its subclasses