Download presentation
Presentation is loading. Please wait.
1
Checked/Uncheck Exception And finally Block
2
Catching Any Exception It is possible to create a handler that catches any kind of exception. (This is possible thanks to upcasting) Treat every possible exception as Exception, which is the base class that’s what programmers are most interested in. Three useful methods in Exception class. –String getMessage() Gets the detailed message –String toString() Overridden method from Object –void printStackTrace() Prints out calling sequence in reverse order.
3
public class ExceptionDemo { ExceptionDemo() { try { thrower(); } catch (Exception e) { System.out.println(“I got it!!!”); System.out.println(“message: “ + e.getMessage()); System.out.println(“more message: “ + e.toString()); e.printStackTrace(); } private void thrower() throws IOException { throw new Exception(“This is harmless exception”); } public static void main(String args[]) { new ExceptionDemo(); }
4
I got it!!! message: This is harmless exception more message: java.io.IOException: This is harmless exception java.io.IOException: This is harmless exception at ExceptionDemo.thrower(ExceptionDemo.java:17) at ExceptionDemo. (ExceptionDemo.java:6) at ExceptionDemo.main(ExceptionDemo.java:21) Output Result
5
Checked/Unchecked Exception As we have seen so far, Java has many different kinds of exceptions. (Recall exception hierarchy) Among all these exceptions, RuntimeException is the one that we do not expect until runtime. RuntimeException can occur anywhere in your program. Therefore the cost of checking often exeeds the benefit of exception handling. Thus compiler does not force you to either specify or catch a RuntimeException. For this reason, RuntimeExeption and all its subclasses are called unchecked exception. All other exceptions are checked exceptions, which means you MUST specify and catch.
6
public class Runtime { Runtime() { thrower(); System.out.println("I will never reach here!"); } private void thrower() { throw new ArrayIndexOutOfBoundsException(); } public static void main(String args[]) { new Runtime(); }
7
Output Result Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at Runtime2.thrower(Runtime2.java:10) at Runtime2. (Runtime2.java:5) at Runtime2.main(Runtime2.java:14)
8
public class Runtime { Runtime() { try { thrower(); }catch (Exception e) { System.out.println("I got it!!!"); System.out.println("message: " + e.getMessage()); System.out.println("more message: " + e.toString()); e.printStackTrace(); } System.out.println(”I finally got here!!!"); } private void thrower() { throw new ArrayIndexOutOfBoundsException(“This is a fake exception); } public static void main(String args[]) { new Runtime(); }
9
Output Result I got it!!! message: This is a fake exception more message: java.lang.ArrayIndexOutOfBoundsException: This is a fake exception java.lang.ArrayIndexOutOfBoundsException: This is a fake exception at Runtime.thrower(Runtime.java:15) at Runtime. (Runtime.java:4) at Runtime.main(Runtime.java:19) I finally got here!!!
10
Finally Block Optionally you can have finally block right after you catch blocks. Whichever statements put inside finally block will be executed with/without occurences of exceptions. It gives programmers last chance to clean up the state of their methods before it gives control to its caller. At this point, you may want to ask youself that Do we really need this? The answer is YES and NO and some people with C++ background are against this finally block. However you will find it extremely useful in some cases, espeically when you deal with I/O.
11
import java.io.*; public class Finally { Finally() throws IOException{ try { thrower(); }catch (IOException e) { System.out.println("I got it!!!\n" + e.getMessage()); throw e; } finally { System.out.println("This should be printed!!!"); } System.out.println("I may not be printed"); } private void thrower() throws IOException { throw new IOException("This is a fake exception"); } public static void main(String args[]) { try { new Finally(); }catch (IOException e) { e.printStackTrace();} }
12
I got it!!! This is a fake exception This should be printed!!! java.io.IOException: This is a fake exception at Finally.thrower(Finally.java:17) at Finally. (Finally.java:5) at Finally.main(Finally.java:22) Output Result
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.