• 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

Catching Exceptions that are not thrown

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this question:

The answer is there is a compile error because BlueException is not thrown anywhere in the try block.
However I answered this incorrectly, because one time I created try/catch blocks around code which did not throw an exception, like this:

I thought maybe I was catching potential RuntimeException with this, but I tried to catch a subclass of Exception that is not a subclass of RuntimeException and I get a compiler error.
Could someone explain this? Thanks.
[ May 30, 2003: Message edited by: Brian J. ]
[ May 30, 2003: Message edited by: Brian J. ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Rule: It is necessary to throw the checked exceptions that are caught.
Exception: The rule does not applies to Exception.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose

Rule: It is necessary to throw the checked exceptions that are caught.


I guess that you probably missed out the word not. According to me the statement should be

Rule: It is necessary to throw the checked exceptions that are not caught.

 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe my wording was confusing. By "caught" I meant to be declared in a catch clause. Though obviously they are not going to be caught if not thrown.
Anyway, Brian posted examples of the rule and exception.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would be the easiest way to resolve Brian's code, please.
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if you use a try/catch block, there must be some object within the try block which can throw the exception you are catching? Ok, I can accept that, it makes sense for a language not to have useless code.
The only exception is that you can try to catch Exception, which is not thrown, without a compiler error?! :-/ Subclasses of Exception don't count obviously...
[ May 31, 2003: Message edited by: Brian Joseph ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brian
The exception to this rule is Exception as earlier pointed out and all unchecked exceptions.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave,
The easiest way to correct this code is to change BlueException in the catch clause to Exception.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little bit confused with the question because of the statement "..I tried to catch a subclass of Exception that is not a subclass of RuntimeException and I get a compiler error."
The way I understand the question is why the code with 'BlueException' does not compile, while the 2nd piece of code (the one where the method does not throw anything) compiles properly
The reason for that is because of this rule:
"The exception in the catch clause must be the same class or superclass of the exception thrown"
The BlueException was rejected simply because it is neither of the same class or superclass of RedException.
The reason why the 2nd piece of code compiles properly is because the 'Exception' class is the superclass of RuntimeException, which is unchecked. And the method "object.method()" can still throw a RuntimeException, which can still be caught.
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anupam, thanks for verifying. I just had to be sure .
reply
    Bookmark Topic Watch Topic
  • New Topic