• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

question on Exception

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The following code compiles fine. But when I un-comment the line '//catch (RedException e) {b++;}', there is compiler error 'exception RedException is never thrown in body of corresponding try statement-catch (RedException e) {b++;}.

My doubt is why is there no compiler error with the catch clause 'catch (Exception e) {c++;}' because the try clause is not throwing any kind of Exception at all.

class GreenException extends Exception {}
class BlueException extends Exception {}
class RedException extends GreenException {}

class White {

void m1() throws RedException {
throw new RedException();
}

public static void main (String[] args) {
White white = new White();
int a,b,c,d; a = b = c = d = 0;
try {
a++;
}
//catch (RedException e) {b++;}
catch (Exception e) {c++;}
finally {d++;}
System.out.print(a+","+b+","+c+","+d);
}
}

Thanks:
Namita
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, it's a Java bug. See this thread...
https://coderanch.com/t/248630/java-programmer-SCJP/certification/Exception-error
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Case of catch(Exception) the compiler assumes that there may be a run time exception in try block and the user wants to handle it.But in case of User defined Exception ,Compiler get confused whether the user is writing Checked Exception or Runtime Exception(unchecked),thats why it varifies and look for the statement in try block which is throwing this RedException for being more specific\Clear.
Thats the reason it gives Comilation Error.It will Applicable to all
Checked Exception like try out writing catch(IOException) , it will give compilation error and now try out catch(NullPointerException)..it will run fine.
Thanks ..Nitin
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi namita,

few days back i also had a simmilar kinda doubt.....

click here to see similar kinda thing :)

thanks
amit
 
reply
    Bookmark Topic Watch Topic
  • New Topic