wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Catching Exceptions that are not thrown Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Catching Exceptions that are not thrown" Watch "Catching Exceptions that are not thrown" New topic
Author

Catching Exceptions that are not thrown

Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
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. ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi.
Rule: It is necessary to throw the checked exceptions that are caught.
Exception: The rule does not applies to Exception.


SCJP2. Please Indent your code using UBB Code
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Jul 03, 2001
Posts: 2120
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.
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
What would be the easiest way to resolve Brian's code, please.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
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

Joined: Apr 13, 2003
Posts: 1088
Hi Brian
The exception to this rule is Exception as earlier pointed out and all unchecked exceptions.
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
Dave,
The easiest way to correct this code is to change BlueException in the catch clause to Exception.


"If anything can go wrong, it will"
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

Joined: May 16, 2003
Posts: 160
Anupam, thanks for verifying. I just had to be sure .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Catching Exceptions that are not thrown
 
Similar Threads
Exception's Question
Dan chisholm - Exceptions
question on Exception
dan exam doubt 15
why do i get a compile error here?