wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Round Up question re exception& arg type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Round Up question re exception& arg type" Watch "Round Up question re exception& arg type" New topic
Author

Round Up question re exception& arg type

Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Good Morning,

I was wondering if anyone can clarify something for me. I do not believe I am violating the copyright rule which I thoroughly support. Please correct me if I am wrong.

There is a Round Up question that reads, "An exception can be caught by a catch block with an argument type that is a superclass of the thrown exception."

The answer is: "If there is no catch statement that matches the thrown exception , then an argument which is a superclass of the exception will catch it."

Frankly, I just don't understand the answer. The word "argument" in particulary is throwing me.

Can someone explain this to me?

Thanks alot!

:-)

Jerry
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
When you have a catch block, you provide an "argument" like this:



In this example, "IOException ioe" and "Exception e" are the "arguments" that are being passed to the exception block.

Say, for example, that our try block throws a RuntimeException, which is a subclass of Exception. There is no catch block for a RuntimeException. However, the way exception handling works is that, if no catch block is found for the given type of exception, the JVM will look for a catch block of any superclasses of the exception thrown. In this case, we have one - Exception. Because a RuntimeException "is a" type of Exception (it extends Excepetion), this makes sense. Therefore, since there is no specific way to handle a RuntimeException, we'll handle it just like we would an Exception.

I hope that helps,
Corey


SCJP Tipline, etc.
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi,

The argument type is a synonym for parameter
type in the case of try/catch blocks just
like in a method's signature.

Suppose there is only a catch(Exception e)
block and in the try block an IOException is thrown,
then then that catch block will be entered.

Cheers,

Gian Franco


"Eppur si muove!"
Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Thank you both for your prompt replies. I understand now.

:-)

Jerry Bustamente
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Round Up question re exception& arg type
 
Similar Threads
Catching non-thrown exceptions
some doubts
////round-up
Rules Round-up (#132)
why constructor needs throws clause