• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Question on Exception Handling

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Level4Exception extends Level3Exception {}

class Test21 {

public static void main(String args[]) {

int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 3;

try {
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
case 4: throw new Level4Exception();
} a++; }
//catch (Level3Exception e) {b++;} // (1)
//catch (Level2Exception e) {b++;} // (2)

//catch (Level1Exception e) {b++;} // (3)
//catch (Exception e) {b++;} // (4)
finally {c++;}
}
catch (Level4Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}

System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}

/*
compile time error by uncommenting line (3) OR line (4) But not by line (1) and line (2).

Even though all four Exceptions(Exception, Level1Exception, Level2Exception, Level3Exception)

are super classes of Level3Exception */
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Girish Nagaraj , it is really nice question,

for checked exception it is necessary that exception should occur in try block and it should propagate upto catch block else error would occur,
for example

try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
}
Here sleep throws checked InterruptedException which is getting propated to catch if you comment sleep then there is no InterruptedException or its subclass which is getting propated to catch so compiler will object it by thinking there is no exception occuring still he is catching it.

coming to your question,you are throwing Level1Exception, if you uncomment line 3 or line 4 it is getting caught in inner catch statements
//catch (Level3Exception e) {b++;} // (1)
//catch (Level2Exception e) {b++;} // (2)

//catch (Level1Exception e) {b++;} // (3)//Here it is getting caught
//catch (Exception e) {b++;} // (4)


and exception is not propagated to outer catch statements
like

catch (Level4Exception e) { d++;}
catch (Exception e) {f++;}
same not happens if you uncomment line 1 or line 2 because it is a failure of catch of inner catch statements and it is propagating to outer catch.

same rules is also applicable to inner try catch but it is satisfying the rules so no error is coming.

i hope you understood it.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did get wat ur trying to say.......

All Exceptions (Exception, Level1Exception, Level2Exception, Level3Exception) are subclasses of Level4Exception so why diff. behaviour with diff. Exceptions commenting???
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think you didnot get, in short you cannot simply catch checked exception with out occuring that checked exception in try block, you are catching it in inner try block so actually no exception is occuring in outer try block ,but you are catching it so error is occuring
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for some cases you are able to catch in inner catch blocks so no exception is occuring in outer try (inner try is also a statement of outer try) but in some cases exception is not getting caught so exception occured which is propagated to catch of outer try,

error occurs in case 1
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it thanks...........
reply
    Bookmark Topic Watch Topic
  • New Topic