17 exceptions

1,052 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,052
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

17 exceptions

  1. 1. Exceptions<br />DhrubojyotiKayal<br />
  2. 2. Guarded section<br />A block where you think some exceptional things – problem with data base connection, network failure, missing data – may happen<br />try{<br /> //code<br />}<br />try block<br />
  3. 3. The thrown exception must end up someplace – exception handler<br />Exception handlers immediately follow the try block and are denoted by the keyword catch<br /> try { <br /> // Code that might generate exceptions <br /> } catch(Type1 id1)|{ <br /> // Handle exceptions of Type1 <br /> } catch(Type2 id2) { <br /> // Handle exceptions of Type2 <br /> } catch(Type3 id3) { <br /> // Handle exceptions of Type3 <br />} <br />Exception Handler<br />
  4. 4. If an exception is thrown, the exception-handling mechanism goes hunting for the first handler with an argument that matches the type of the exception <br />Then it enters that catch clause, and the exception is considered handled <br />The search for handlers stops once the catch clause is finished. Only the matching catch clause executes (unlike switch – no need for break or default)<br />Sometimes one handler can be sufficient.<br />Catch block<br />
  5. 5. To create your own exception class, you must inherit from an existing exception class, preferably one that is close in meaning to your new exception <br />class SimpleException extends Exception {} <br />Creating your own exceptions<br />
  6. 6. public class InheritingExceptions { <br />public void f() throws SimpleException { <br />System.out.println("Throw SimpleException from f()"); <br />throw new SimpleException(); <br />} <br />public static void main(String[] args) { <br />InheritingExceptionssed = new InheritingExceptions(); <br />try { <br />sed.f(); <br />} catch(SimpleException e) { <br />System.out.println("Caught it!"); <br />e.printStackTrace(); <br />} <br />} <br />} <br />printStackTrace – shows sequence of methods called and where exactly in execution the exception was raised making it easier to pin point exception source.<br />Using your own exception<br />
  7. 7. In Java, you’re encouraged to inform the client programmer, who calls your method, of the exceptions that might be thrown from your method <br />This is civilized, because the caller can then know exactly what code to write to catch all potential exceptions <br />Add throws followed by Exception names to tell the world the problems your code may cause<br />Throwing Exceptions<br />
  8. 8. void f() throws TooBig, TooSmall, DivZero { //... <br />void f() { //... <br />This is not safe and can be vulnerable to RuntimeException and its likes<br />Throwing Exceptions<br />
  9. 9. It is possible to create a handler that catches any type of exception by catching the base-class exception type Exception<br />catch(Exception e) { <br />System.out.println("Caught an exception"); <br />} <br />Since the Exception class is the base of all the exception classes that are important to the programmer, you don’t get much specific information about the exception, but you can call the methods that come from its base type Throwable<br />String getMessage( ) <br />String getLocalizedMessage( ) <br />void printStackTrace( ) <br />void printStackTrace(PrintStream) <br />void printStackTrace(java.io.PrintWriter)<br />Catching any Exception<br />
  10. 10. Write a Java code which first has an exception handler for Exception class followed by another exception handler for custom MyClass<br />Excercise<br />
  11. 11. The Java class Throwable describes anything that can be thrown as an exception <br />Error represents compile-time and system errors that you don’t worry about catching <br />Exception is the basic type that can be thrown from any of the standard Java library class methods and from your methods and runtime accidents. So the Java programmer’s base type of interest is usually Exception. <br />This are called checked exceptions<br />Standard Java Exceptions<br />
  12. 12. RuntimeException and its children have special meaning to the runtime. <br />They cause hazards without intimation – you do not declare them in the method exception specification<br />They generally results of programming errors<br />Use runtime exception if a you cannot recover from an operation<br />If you are handling/catching RuntimeException there is problem in your code and design<br />Unchecked Exceptions<br />
  13. 13. public class NeverCaught { <br />static void f() { <br />throw new RuntimeException("From f()"); <br />} <br />static void g() { <br />f(); <br />} <br />public static void main(String[] args) { <br />g(); <br />} <br />} <br />RuntimeException in Action<br />
  14. 14. There’s often some piece of code that you want to execute whether or not an exception is thrown within a try block <br />To achieve this effect, you use a finally clause at the end of all the exception handlers <br />try { <br />// The guarded region: Dangerous activities <br />// that might throw A, B, or C <br />} catch(A a1) { <br />// Handler for situation A <br />} catch(B b1) { <br />// Handler for situation B <br />} catch(C c1) { <br />// Handler for situation C <br />} finally { <br />// Activities that happen every time <br />} <br />And finally<br />
  15. 15. class ThreeException extends Exception {} <br />public class FinallyWorks { <br /> static int count = 0; <br /> public static void main(String[] args) { <br /> while(true) { <br />try { <br />// Post-increment is zero first time: <br />if(count++ == 0) <br />throw new ThreeException(); <br />System.out.println("No exception"); <br />} catch(ThreeException e) { <br />System.out.println("ThreeException"); <br />} finally { <br />System.out.println("In finally clause"); <br />if(count == 2) break; // out of "while" <br />} <br />} <br />} <br />Finally in Action<br />
  16. 16. public class MultipleReturns { <br /> public static void f(inti) { <br /> print("Initialization that requires cleanup"); <br /> try { <br /> print("Point 1"); <br /> if(i== 1) return; <br /> print("Point 2"); <br /> if(i== 2) return; <br /> print("Point 3"); <br /> if(i== 3) return; <br /> print("End"); <br /> return; <br /> }finally { <br /> print("Performing cleanup"); <br /> } <br /> } <br /> public static void main(String[] args) { <br /> for(int i = 1; i <= 4; i++) <br /> f(i); <br /> } <br />} <br />finally and return<br />
  17. 17. Q&A<br />

×