• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Round Up question re exception& arg type

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for your prompt replies. I understand now.

:-)

Jerry Bustamente
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic