Exceptionn
Upcoming SlideShare
Loading in...5
×
 

 

Statistics

Views

Total Views
324
Views on SlideShare
324
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Exceptionn Exceptionn Presentation Transcript

  • STORED PROCEDURE
  • TRY CATCH & EXCEPTION HANDLING RAJISHMA T. 26/04/13
  • 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.
  • Exception classes • The figure below shows the hierarchy of the Exception classes.
  • 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 .
  • Exception handling process
  • Catching & handling exceptions • 3 exception handler components try catch finally
  • 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 . . .
  • 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.
  • 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.
  • 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); } }
  • 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); } }
  • • i= 0 i= 1 i= 2 i= 3 i= 4 i= 5 i= 6 Yikes! i= 7 Yikes! i= 8 Sum: 23
  • 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{ }
  • • Eg: public class Security extends Exception { public Security() { super("Attempted Security Breach - User attempted to breach security"); } }
  • Thank you