• 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

Assertion Exception handling

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
How can following code compile?
Iam asking this because,this code block is trying to catch an Exception which is never thrown[AssertionError is NOT a child of Exception].But code still compiles

Why is that?

Many Thanks

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Exception class is also the base class for the RuntimeException class. The compiler is allowing the Exception here because it can't tell if it "is trying to catch an Exception which is never thrown" -- it could be an unchecked exception that is caught.

Henry
 
prashanth kumar
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification.
This means that a called method[even if it doesnt throw any chcecked exceptions] can always be there in try-catch block of calling method with "Exception" catch, since RunTimeException is a subclass of Exception.
Ex:


I tested this and it works.

Thanks Again
Prashanth
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prashanth,

Including any code in the try block does not mean that the code should throw an exception.

Compiler only cares about checked Exceptions and since AssertionError is a runtime Error(not an exception, it does not even come under Exception heirarchy ), the compilation proceeds even if you don't deal with it.

Moreover it is not a good practice to handle Assertion errors(As they can be disabled when the code compiles or runs), it is meant to use only during development not during deployment.

- Ramu
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What can you catch when try block does not throw exception?
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic