Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Queston on exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Queston on exceptions" Watch "Queston on exceptions" New topic
Author

Queston on exceptions

Sandya Bhaskara
Ranch Hand

Joined: Aug 20, 2004
Posts: 73
public class A{
void method(){
try {
meth();}catch (RuntimeException e){}
catch (Exception e){}
}
}
void meth(){

throw new RuntimeException()
}
}


will this code compile??


Sandy<br />SCJP1.4 SCWCD1.4<br />SCBCD(preparing)
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
No, but that would have been easy enough to test. The following does compile:


Is there a question here you are looking for an answer to?
Sandya Bhaskara
Ranch Hand

Joined: Aug 20, 2004
Posts: 73
here...meth()doesn't throw any checked exceptions..and method() is catching the only runtime exception that meth() throws..so why doesn't the compiler give an error?- catching an exception(here---catch (Exception e)) that has never been thrown??any exception that isnt a checked exception is a runtime exception...and that is caught by the code..so is it ok to catch Exception again??am i clear?
Thana Bujakorn
Greenhorn

Joined: Dec 18, 2002
Posts: 12
In my opinion, it should compile just fine.


It doesn't seem to violate any of the exception rules. You must declare that a method "throws" exception or provide a catch clause for it only if it is checked exception. RuntimeException and its subclasses are considered unchecked.
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
It won't compile without adding a semicolon the following line:
throw new RuntimeException()

but I don't think that is the answer you were really looking for...
Sandya Bhaskara
Ranch Hand

Joined: Aug 20, 2004
Posts: 73
nopes..its not the semicolon thing......that was a typo...
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Beside the semicolon typo Chris mentioned, there is an extra bracelet on line 6 that should be removed.

The modified code should look like this



You may want to use proper indentation next time in order to make your code more clear and readable, consider also using the [code] tags.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Queston on exceptions
 
Similar Threads
help needed
exception handling
Exception doubt
throwing RuntimeException with null value
exception handling question