Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Exception question!!

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we specify a catch clause for an exception not thrown in the try block?. How does the compiler make sure that the try block will not raise the exception thatz being specified in one of the catch CLAUSE.
the below example gives a COMPILER ERROR since the try block doesn't throw the BLUEEXCEPTION but is being caught in one of the CATCH clause.

class RedException extends Exception {}
class BlueException extends Exception {}
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 {white.m1(); a++;}
catch (RedException e) {b++;}
catch (BlueException e) {c++;}
finally {d++;}
System.out.print(a+","+b+","+c+","+d);
}}
clarify ~ Shalini
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup, you can not catch any CHECKED exception which are not thrown inside your try block, compiler is smart enough to realize that.
But, you can catch exception which are subclasses of RuntimeException class, since the compiler does not know whether they will be thrown or not (ex.: NullPointerException)
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can safely mention the generic Exception class in the catch block even though no exception was thrown in the try block.
Rama
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Taurean,
The compiler insist u to give a try catch block if u have a call
to the method which throws a checked exception.The second case in when there is a call to the method which throws the exception(in this case mi()).Compiler doesn't make sure that the exception that u declare in the catch block is occuring the the try block or not but it do make sure that the order in which they come in the try block is in the order(subclass should come first).
In the question that u have asked the reason for the error is that BlueException is coming after the RedException so BlueException will never reach.
If u would like to make any correction u r always welcome,Taurean. :roll:
class RedException1 extends Exception {
}
class BlueException1 extends Exception {
}
class White1 {
void m1() throws RedException1 {
throw new RedException1();
}
public static void main (String[] args) {
White1 white = new White1();
int a,b,c,d; a = b = c = d = 0;
try {
white.m1();
a++;
}
catch (BlueException1 e) {
c++;
}
catch (RedException1 e) {
b++;
}

finally {
d++;
}
System.out.print(a+","+b+","+c+","+d);
}
}

*/
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singh,
what you said is true IF RED is a supeclass of BLUE but here is not the case...
here you woul get an error like this IF
....
catch (Exception e){}
catch (Red e){}
...
 
Harvinder Singh
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U r right Dan, In that I just overlooked it and assumed it be the subclass.
this habit of not reading code properly is going to cost me a lot in the certification exam. :roll:
 
Screaming fools! It's nothing more than a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic