Exceptions in Programming in Java | CSCI 2220, Study notes of Javascript programming

Material Type: Notes; Class: PROGRAMMING IN JAVA; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Spring 2005;

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-cs6
koofers-user-cs6 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming in Java
CSCI-2220
Exceptions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Exceptions in Programming in Java | CSCI 2220 and more Study notes Javascript programming in PDF only on Docsity!

Programming in Java

CSCI-

Exceptions

Exceptions

•^

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

Throwing Exceptions

•^

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;

} }

Catching Exceptions

•^

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(); }

RunTimeException

•^

These are the subclasses of the RunTimeExceptionclass–

ArithmeticException– IndexOutOfBoundsException– NegativeArraySizeException– NullPointerException– ArrayStoreException– ClassCastException– IllegalArgumentException– SecurityException– IllegalMonitorStateException– IllegalStateException– UnsupportedOperationException

Dealing with Exceptions

•^

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 }

Multiple catch blocks

•^

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(); }

The finally block

•^

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 }

Exceptions

•^

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

Homework 3

• Homework 3 is out• Due February 16• Homework 2 extended to Friday 11:55 PM