Exception Handling Updated
Exception Handling Updated
Executable
Linking image
compiler
loader
compilation
Execution
1980’s : two tier architecture or server-client
architecture
Program creation
Server Client
compilation
Execution
Execution
1990’s : n-tier architecture or Internet
architecture Execution
Program creation
Server
compilation Internet
Execution
Execution
Execution
• Exception handling is very much important
when it comes to java programming coz if
exceptions are not handled then it would put
so many users under trouble.
c-program c-program c-program
OS OS OS
System Crash System Crash System Crash
java-program java-program java-program
R.T.S R.T.S
OS OS
OS
s.o.p(“enter numerator”);
int a=Scan.nextInt(); Valid input hence
s.o.p(“enter denominator”); Valid output
int b=Scan.nextInt(); internet
int c=a/b;
s.o.p(c);
Enter numerator
100
Enter denominator
0
Exception
To overcome the above issue we should add the User defined exception handler as shown
in the next slide.
• Whenever exception is generated a method in
which exception has occurred will create an
exception object and hands it over to runtime.
• RTS will search for user defined exception
handler and if it is found it will handover
exception object to UDEH and if it is not found
it would handover the exception object to
DEH.
Class Demo
{psvm(---)
{
Scanner s=new Scanner(System.in);
SOP(“connection is established);
Try{
SOP(“enter numerator”);
Int n=scan.nextInt();
SOP(“enter denominator”);
Int d=scan.nextint();
Int res=n/d;
SOP(res);
}
catch(exception e)
{
SOP(“Invalid input”);
}
SOP(“connection terminated”);
}
}
Whenever exception occurs
UDEH DEH
Class Demo Int val=scan.nextInt();
{psvm(---) SOP(“enter the position”);
{ Int i=scan.nextInt();
Scanner s=new Scanner(System.in); arr[i]=val;
SOP(“connection is established); SOP(“successfully inserted”);
}
try{
SOP(“enter numerator”); Catch(exception e)
Int n=scan.nextInt(); {
SOP(“enter denominator”); SOP(“Invalid input”);
Int d=scan.nextint(); }
Int res=n/d; SOP(“connection terminated”);
SOP(res); }
SOP(“enter the size of array”); }
Int size=scan.nextInt();
Int[] arr=new [size];
SOP(“enter the value”);
• NOTE: The advantage of DEH is that it will display
the correct/appropriate message according to the
mistake committed by the user however the
disadvantage of DEH is it will not terminate the
connection.
• the advantage of UDEH is it will terminate the
connection however the disadvantage is for every
type of mistake it will display the same msg.
• the problem associated with single catch block
can be overcome by using multiple catch blocks
as shown in below/next slide
Class Demo }
{ Catch(ArithmeticException e)
Psvm(-----) {
{ Sop(“don’t enter 0 as denominator”);
Scanner scan=new Scanner(System.in); }
Sop(“connection established”); Catch(ArrayOutOfBoundsException f)
Try{ {
Sop(“enter the numerator; Sop(“be in your limit”);
Int n=scan.nextInt(); }
SOP(“enter denominator”); Catch(NegativeSizeArrayException g)
Int d=scan.nextint(); {
Int res=n/d; Sop(“Be positive”);
SOP(res); }
SOP(“enter the size of array”); Catch(Exception h)
Int size=scan.nextInt(); {
Int[] arr=new [size]; Sop(“some problem occurred”);
SOP(“enter the value”); }
Int val=scan.nextInt(); Sop(“connection terminated”);
SOP(“enter the position”); }
Int i=scan.nextInt(); }
arr[i]=val;
SOP(“successfully inserted”);
NOTE
1)as a developer we should anticipate all type of mistake that
a user may commit and accordingly specific catch block should
be provided.as a precautionary measure a generic catch block
should be included at the bottom of all the specific catch
block.
• if none of the specific catch blocks can handle the
exception then the generic catch block will handle.
2)Exception type refers to all type of exceptions coz exception
is the parent for all exceptions.
3)a generic catch block should never be included at the top of
other catch block because generic catch block has the ability
to catch all type of exceptions.
Class Demo1 Demo2 d2=new Demo2();
{ d2.fun2();
Void fun1() }
{ }
Scanner scan=new Scanner(System.in);
Sop(“enter the numerator”); Class Launch
Int n=scan.nextInt(); {
Sop(“enter the denominator”); Psvm(------)
Int d=scan.nextInt(); {
Int res=n/d; Sop(“connection established”);
Sop(res); Demo3 d3=new Demo3();
} d3.fun3();
} Sop(“connection terminated”);
Class Demo2 }
{ }
Void fun2() o/p
{ Exception in thread “main”
Demo1 d1=new Demo1(); java.lang.ArithmeticException / by zero
d1.fun1(); At Demo1.fun1(launch………. Java 12)
} At Demo2.fun2(launch………. Java 26)
} At Demo3.fun3(launch………. Java 33)
Class Demo3 At Launch.main(launch………. Java 42)
{
Void fun3()
{
Class Launch
{
Psvm(------)
{
Sop(“connection established”);
Try
{
Demo3 d3=new Demo3();
d3.fun3();
}
Catch (ArithmeticException e)
{
Sop(“problem resolved in main()”);
}
Sop(“connection terminated”);
}
}
fun1()
fun1()
{
R.T.S {
R.T.S
} No UDEH No UDEH
}
fun2()
fun2()
{ No UDEH { No UDEH
}
}
fun3()
fun3()
{
{
No UDEH
No UDEH
}
}
main()
main()
{
No UDEH {
UDEH
}
}
D.E.H
D.E.H
OS OS
NOte:
• 1)if exception occurs in one of the activation records
and if there is no UDEH in that A.R then the RTS will
not immediately hand it over to the DEH rather it will
propagate the exception object to check if there is any
handler provided in remaining A.R. if none of the A.R
contain UDEH only then the exception object will be
given to DEH.
• 2)if there is a handler provided in any one of A.R then
Excception object will be given to that handler.
• There are three ways of managing an
exception
– Handling an exception(using try&catch )
– Rethrowing an exception(using try , catch,
throw,throws & finally)
– Ducking an exception(using try, catch & throws)
Handling an exception(using try&catch )
class Demo1
{
void fun3()
{ class LaunchDemo1
System.out.println("connection established"); {
try
{ public static void main(String args[])
System.out.println("enter the numerator"); {
Scanner scan=new Scanner(System.in);
int a=scan.nextInt();
Demo1 d1=new Demo1();
System.out.println("enter the denominator"); d1.fun3();
int b=scan.nextInt(); }
int c=a/b;
System.out.println(c); }
}
catch (Exception e)
{
System.out.println("invalid inputs");
}
System.out.println("connection terminated");
}
}
fun3()
{
conn2 established
R.T.S
UDEH
conn2 terminated
} Handling an exception
main()
{
D.E.H
• In the above program exception occurred in fun3
method and the exception was handled in fun3
method only.
• the main method is not aware of the exception
that occurred in fun3 method this is because
whenever exception is handled the information
about an exception will not be automatically
propagated to other methods.
• whenever we want to propagate an exception
object then we have to make use of throw
keyword.
Example why throwing an exception is
important
Bank
Branch1 Branch2
class Demo1
{
void fun3() throws Exception
{
class LaunchDemo1
System.out.println("connection2 established"); {
try{ public static void main(String args[])
System.out.println("enter the numerator");
{
Scanner scan=new Scanner(System.in);
int a=scan.nextInt();
System.out.println("connection2 established");
System.out.println("enter the denominator"); Demo1 d1=new Demo1();
int b=scan.nextInt(); d1.fun3();
int c=a/b;
System.out.println("connection2 terminated");
System.out.println(c);
}
catch (Exception e){ }
System.out.println("invalid inputs"); }
throw e;
}
finally{
System.out.println("connection2 terminated");}
}
}
• the disadvantage of throw keyword is lines
below the throw keyword will not get
executed, however we can overcome this
problem by using finally block. If there are any
critical statement present in program which
compulsorily have to get executed such
statements should be enclosed within finally
block. The finally block should get executed
even if there is a return statement.
fun3()
{
conn2 established
R.T.S
UDEH(throw an exception)
conn2 terminated
} rethrowing an exception
main()
{
conn1 established
R.T.S
UDEH
conn1 terminated
}
D.E.H
Ducking an exception(using try&catch )
class Demo1
{
void fun3() throws Exception
{
class LaunchDemo1
System.out.println("enter the numerator"); {
Scanner scan=new Scanner(System.in); public static void main(String args[])
int a=scan.nextInt();
{
System.out.println("enter the denominator");
int b=scan.nextInt();
System.out.println("connection2 established");
int c=a/b; Demo1 d1=new Demo1();
System.out.println(c); d1.fun3();
System.out.println("connection2 terminated");
System.out.println("connection2 terminated");
}
}
}
}
fun3()
{
R.T.S
} ducking an exception
main()
{
conn1 established
R.T.S
UDEH
conn1 terminated
}
D.E.H
• Rethrowing an exception refers to the process
of handling an exception and then
propagating the exception object to other
methods.
• Ducking an exception refers to the process of
passing the exception object to other methods
without handling it
class Demo1
{
void disp()
{
disp()
}
}
Class LaunchDemo
{
p s v m()
{
Demo1 d1=new Demo1();
d1.disp();
}
}
Above program leads to StackOverflowError.
stack
fun()
{
}
fun()
{
}
fun()
{
}
fun()
{
}
main()
{
}
Difference between runtime error and
exception
• Runtime error Exceptions
• They are the mistakes Exception refers to
That occur during the the mistakes that occur
Execution time due to due to the faulty inputs.
Improper logic.
They cannot be managed they can be managed by
by using try and catch. Using try and catch.
Eg:StackOverflowError, InputMismatchException
OutOfMemoryError NegativeArraySizeException
Object(class)
Throwable(class)
Error Exception
InputMismatchException FileNotFoundExceptio
• unchecked exceptions are such exceptions
where the compiler doesn’t force the
programmer to handle an exception.
• checked exceptions are such exceptions where
the compiler forces the programmer to handle
an exceptions.
difference between throw keyword and throws
keyword.
• throw keyword is used for rethrowing an
exception||throws is used for ducking an
exception
• throw keyword is used within the body of the
method||is used in the signature of the method
• the lines below the throw keyword will not get
executed||the lines below the throws keyword
will get executed.
LSP(Lisko Substitution Principles)
Rule no :1
1)If a method in parent class throws an exception then it is not compulsory for the
overridden method in child class also to throw the same exception.
class Demo1{
void disp() throws ArithmeticException
{}
}
class Demo1{
void disp() throws ArithmeticException
{}
}
class Demo1{
void disp() throws Exception
{}
}
InvalidCustomerException