Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Exception hierarchy

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why compiler doesnt enforce RuntimeException handling?
RuntimeException and IOException are subclasses of Exception. We are forced to handle IOException but not RuntimeException.
When I looked into the API source code for both these classes, theres no difference apart from class & constructor names.
Though exception handling requirement is not enforced programatically in the any of these classes, how this difference comes up?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between checked and unchecked exceptions is defined in the Java language specification. RuntimeException and all subclasses of it are unchecked exceptions. That's just how it's defined in the specification and the Java compiler verifies that Java source code is written by those rules.

There's nothing special about how RuntimeException is declared, so you can't know it by just looking at the API declaration or the source code of class RuntimeException. The Java compiler handles it as a special case.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's part of the Java Language Specification that RuntimeException is treated differently - you can look it up there.
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If not in the source code, where would this rule be defined?
 
Marshal
Posts: 69748
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the Java Tutorials? That might explain it.

Otherwise: the compiler is programmed to look at the inheritance hierarchy of classes. It has to look at the Exception hierarchy to see whether a class or class name can be used after the keywords throw, throws and catch. Similarly it looks to see whether an Exception a Throwable is a subclass of Error or RuntimeException (=unchecked), otherwise assuming it is checked.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Larsen Raja wrote:If not in the source code, where would this rule be defined?


As Matthew said: The Java Language Specification, which is where you should look first for ALL language rules. And, as Campbell said, any decent tutorial should also include it.

I'm also a little worried that you're looking at source code for this sort of information. You should always look at the documentation first (starting with the API docs for the class), because source code might well change.

Winston

BTW: Happy New Year everybody.
 
Campbell Ritchie
Marshal
Posts: 69748
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . BTW: Happy New Year everybody.

Thank you Same to you.
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. I just hold less than a year experience in Java. I am just preparing for SCJP 6 and so I never reffered API documentation. I just use Kathy' SCJP Book.
All above information helped, thanks again.

->Happy New Year All<-
Larsen
 
Campbell Ritchie
Marshal
Posts: 69748
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Larsen Raja wrote:Thank you guys . . .
->Happy New Year All<-
Larsen

You’re welcome
 
Campbell Ritchie
Marshal
Posts: 69748
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Larsen Raja wrote: . . . I just hold less than a year experience in Java. . . . I never reffered API documentation. . . .


You have been very badly taught if you have not been taught to find the documentation.
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always thought that documentations are tough to learn from. And also one more qns, will going through API documentation make it easier to get SCJP cert?
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic