• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Exception Catching

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this code:
try {
//Thread.sleep(600);
} catch (InterruptedException x) {}
It gives compilation error as:
exception java.lang.InterruptedException is never thrown in body of corresponding try statement.

But,
try {
//Thread.sleep(600);
} catch (Exception x) {} // This one gives no error.
Why is this so?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beause compiler is smart enough to think that , while at run time the code might generate runtime exceptions like ArrayIndexoutOfBounds, Nullpointer etc .. hence the second code compiles. While in the first code you are cathing a checked exception
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the compiler doesnt object for

Throwable , Exception,( Error & RuntimeException and its subclasses )
( to be given in the catch clause ) when try is given as :

try {
}
catch( ) { }

however it objects for All CHECKED Exceptions ...

(Correct me if am wrong)

Hema
 
Arnab Saha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I have tested that if in catch block Unchecked exceptions(like ArithmeticException)are given then there is no problem.
But Exception is Super class of Unchecked Exceptions.
So,like checked exceptions it should also generate error.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

For checked exceptions it is imperative that you put your code that would generate the checked exception in a try block and a corresponding catch block should catch it...

If you put a try catch block and dont specify the code that generates the
checked exception ..it would give compile error

For unchecked exceptions it isnt the way because they happen at runtime

You can write or you can ignore writing a try catch block for it.

Ex:
class A{
{
int i=9/0;
}
}

But if you are going to throw its different...you need to put it in a try catch block else doesnt compile

Compiles fine
Class A{
{
try{
throw new RuntimeException();
}catch (Exception e) {

}
}
}
Doesnt compile

Class A{
{
throw new RuntimeException();
}

}
Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic