Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Exceptionn

236 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Exceptionn

  1. 1. STORED PROCEDURE
  2. 2. TRY CATCH & EXCEPTION HANDLING RAJISHMA T. 26/04/13
  3. 3. EXCEPTIONS • An exception is an event,which occurs during the execution of a program,that disrupts the normal flow of the programs instruction. • When an error occurs within amethod,the method creates an object and hands it off to the runtime system. • Creating an exception object and handing it to the runtime system is called throwing an exception.
  4. 4. Exception classes • The figure below shows the hierarchy of the Exception classes.
  5. 5. Checked vs unchecked • the subclasses of Error and RuntimeException are known as unchecked exceptions. • These exceptions are not checked by the compiler, and hence, need not be caught or declared to be thrown in your program. • This is because there is not much you can do with these except. • All the other exception are called checked exceptions. • They are checked by the compiler and must be caught or declared to be thrown .
  6. 6. Exception handling process
  7. 7. Catching & handling exceptions • 3 exception handler components try catch finally
  8. 8. try • The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block. try { code } catch and finally blocks . . .
  9. 9. Catch • You can associates exception handlers with a try block by providing one or more catch block directly after the try block. try { } catch(exception type name){} catch(exception type name){} Each catch block is an exception handler & handles the type of exception indicated by its argument.
  10. 10. Finally block • The finally block always execute when the try block exits • This ensures that the finally blockis executed even if an unexpected exception occurs. • Whwt the finally will do is contain some code ,whether or not an exception is thrown,that code will execute.
  11. 11. Try catch eg: public class TC { public static void main(String[] args) { int a=10; int b=5,c=5; int x,y; try { x = a / (b-c); } catch(ArithmeticException e) { System.out.println("Divide by zero"); } y = a / (b+c); System.out.println("y = " + y); } }
  12. 12. Eg 2: • public class TryExample { public static void main(String args[]) { String str = "2346512aa"; int sum = 0; for(int i = 0; i < str.length(); i++) { try { sum += Integer.parseInt(str.charAt(i)+""); } catch(NumberFormatException nfe) { System.out.print("Yikes! "); } finally { System.out.println("i= " + i); } } System.out.println("Sum: " + sum); } }
  13. 13. • i= 0 i= 1 i= 2 i= 3 i= 4 i= 5 i= 6 Yikes! i= 7 Yikes! i= 8 Sum: 23
  14. 14. Declaring your own exceptions • All exceptions must be a child of Throwable. • If you want to write a checked exception you need to extend the Exception class. • If you want to write a runtime exception, you need to extend the RuntimeException class. • We can define our own Exception class as below: class MyException extends Exception{ }
  15. 15. • Eg: public class Security extends Exception { public Security() { super("Attempted Security Breach - User attempted to breach security"); } }
  16. 16. Thank you

×