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

Exceptionn

  • 1.
  • 2.
    TRY CATCH &EXCEPTION HANDLING RAJISHMA T. 26/04/13
  • 3.
    EXCEPTIONS • An exceptionis 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.
    Exception classes • Thefigure below shows the hierarchy of the Exception classes.
  • 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.
  • 7.
    Catching & handlingexceptions • 3 exception handler components try catch finally
  • 8.
    try • The firststep 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.
    Catch • You canassociates 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.
    Finally block • Thefinally 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.
    Try catch eg: publicclass 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.
    Eg 2: • publicclass 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.
    • i= 0 i=1 i= 2 i= 3 i= 4 i= 5 i= 6 Yikes! i= 7 Yikes! i= 8 Sum: 23
  • 14.
    Declaring your ownexceptions • 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.
    • Eg: public classSecurity extends Exception { public Security() { super("Attempted Security Breach - User attempted to breach security"); } }
  • 16.