We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17
EXCEPTION HANDLING
class Exc2 { class Exc2 {
public static void main(String args[]) { public static void main(String args[]) { int d, a; int d, a; try { // monitor a block of code. try { // monitor a block of code. d = 0; d = 0; a = 42 / d; a = 42 / d; System.out.println("This will not be System.out.println("This will not be printed"); printed"); } } catch (ArithmeticException e) { catch (ArithmeticException e) { System.out.println("Division by zero."); System.out.println("Division by zero."); a = 0; } System.out.println("After catch statement."); } } System.out.println("After catch } statement."); } class MultiCatch { catch(ArrayIndexOutOfBoundsException e) { public static void main(String args[]) System.out.println("Array index oob: " + e); } { System.out.println("After try/catch blocks."); try { } int a = args.length; } System.out.println("a = " + a); C:\>java MultiCatch int b = 42 / a; a=0 int c[] = { 1 }; Divide by 0: java.lang.ArithmeticException: / by c[42] = 99; zero } After try/catch blocks. catch(ArithmeticException e) { C:\>java MultiCatch TestArg System.out.println("Divide by 0: " + e); a=1 } Array index oob: java.lang.ArrayIndexOutOfBoundsException:42 class SuperSubCatch { public static void main(String args[]) { try { int a = 0; int b = 42 / a; } catch(Exception e) { System.out.println("Generic Exception catch."); }
/* This catch is never reached because ArithmeticException is a subclass of Exception. */
System.out.println("This is never reached."); } } } class NestTry { catch(ArrayIndexOutOfBoundsException e) public static void main(String args[]) { try { { int a = args.length; System.out.println("Array index out-of- int b = 42 / a; bounds: " + e); System.out.println("a = " + a); } try { } if(a==1) a = a/(a-a); catch(ArithmeticException e) if(a==2) { { int c[] = { 1 }; System.out.println("Divide by 0: " + e); c[42] = 99; } } } } } throw It is possible to throw an exception explicitly, using the throw statement. The general form of throw is shown here:
throw ThrowableInstance;
Here, ThrowableInstance must be an object of type Throwable or a
subclass of Throwable. class ThrowDemo { public static void main(String args[]) static void demoproc() { { try { try { throw new demoproc(); } NullPointerException("demo"); catch(NullPointerException e) { } System.out.println("Recaught: " + e); catch(NullPointerException e) { System.out.println("Caught inside } demoproc."); } throw e; // rethrow the exception } } } Caught inside demoproc. Recaught: java.lang.NullPointerException: demo throws A throws clause lists the types of exceptions that a method might throw. This is necessary for all exceptions or RuntimeException, or any of their subclasses. All other exceptions that a method can throw must be declared in the throws clause. If they are not, a compile-time error will result. type method-name(parameter-list) throws exception-list { // body of method }
Here, exception-list is a comma-separated list of the exceptions that a
method can throw. class ThrowsDemo class ThrowsDemo { { static void throwOne() throws static void throwOne() { IllegalAccessException { System.out.println("Inside System.out.println("Inside throwOne."); throwOne."); throw new IllegalAccessException("demo"); throw new } IllegalAccessException("demo"); } public static void main(String args[]) { try { public static void main(String args[]) throwOne(); } { catch (IllegalAccessException e) { throwOne(); System.out.println("Caught " + e); } } } } } finally • finally creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. • The finally block will execute whether or not an exception is thrown. • If an exception is thrown, the finally block will execute even if no catch statement matches the exception. • Any time a method is about to return to the caller from inside a try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also executed just before the method returns. • This can be useful for closing file handles and freeing up any other resources that might have been allocated at the beginning of a method with the intent of disposing of them before returning. • The finally clause is optional. • However, each try statement requires at least one catch or a finally clause. class FinallyDemo { static void procC() { static void procA() { try { System.out.println("inside procC"); try { } System.out.println("inside procA"); finally { throw new RuntimeException("demo"); } System.out.println("procC's finally"); finally { } System.out.println("procA's finally"); } } } public static void main(String args[]) { static void procB() { try { procA(); } try { catch (Exception e) { System.out.println("inside procB"); System.out.println("Exception caught"); return; } } finally { procB(); System.out.println("procB's finally"); } procC(); } } } Unchecked RuntimeException Subclasses Checked Exceptions Creating Your Own Exception Subclasses • We will probably want to create our own exception types to handle situations specific to your application. • This is quite easy to do: just define a subclass of Exception. • The Exception class does not define any methods of its own. It does, of course, inherit those methods provided by Throwable. • Thus, all exceptions, including those that you create, have the methods defined by Throwable available to them. class MyException extends Exception System.out.println("Normal exit"); { private int detail; } MyException(int a) { detail = a; } public static void main(String args[]) { public String toString() try { { return "MyException[" + detail + "]"; } compute(1); } compute(20); class ExceptionDemo { } static void compute(int a) throws catch (MyException e) { MyException { System.out.println("Caught " + e); } System.out.println("Called compute(" } + a + ")"); } if(a > 10) Called compute(1) Normal exit throw new MyException(a); Called compute(20) Caught MyException[20] Chained Exceptions catch(NullPointerException e) class ChainExcDemo { { static void demoproc() { // display top level exception NullPointerException e = System.out.println("Caught: " + e); new NullPointerException("top layer"); // add a cause // display cause exception e.initCause(new System.out.println("Original cause: " ArithmeticException("cause")); + e.getCause()); throw e; } } } public static void main(String args[]) { } try { demoproc(); } Caught: java.lang.NullPointerException: top layer Original cause: java.lang.ArithmeticException: cause
Chapter 02 Computer Organization and Design, Fifth Edition: The Hardware/Software Interface (The Morgan Kaufmann Series in Computer Architecture and Design) 5th Edition