File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Round Up question re exception& arg type

 
Jerry Bustamente
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for your prompt replies. I understand now.

:-)

Jerry Bustamente
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic