wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes A Question About Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A Question About Exception" Watch "A Question About Exception" New topic
Author

A Question About Exception

ravel chen
Greenhorn

Joined: Mar 03, 2004
Posts: 3
Here is the code

Compiling it,we got
0,0,0,1,1
Obviously,If we remove Line 17
We got compiling error:
unreported exception ColorException; must be caught or declared t
o be thrown.
Removing 16 and remain Line 17
Compile OK! We got
0,1,0,1,0
We know we should consider inherit relationship of Exception.
And I am not surprising the two above-mentioned results.
My Question is
Since we put LIne 16 before Line17(as you see),
why doesn't compiling error whose statement is unreported exception ColorException (like above mentioned)?
[ edited to preserve formatting using the [code] and [/code] UBB tags, and to add a little formatting -ds ]
[ May 11, 2004: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Since we put LIne 16 before Line17(as you see),
why doesn't compiling error whose statement is unreported exception ColorException (like above mentioned)?

Since WhiteException is a subclass of ColorException (so, WhiteException is a sub-type of ColorException), a catch block for a ColorException, could also catch a WhiteException. A catch block for a ColorException could catch any exception that is a sub-type of ColorException.
The rule is, a catch block, for some type A can, can catch any exception of type A, as well as any exception of a sub-type of A.
If you're looking for more resources to better learn the basics of exception handling, I recommend reading
  • chapter 9 of Bruce Eckel's Thinking In Java Book
  • chapter 9 of David J. Eck's Introduction to Programming Using Java
  • chapters 80 & 81 of Bradley Kjell's Introduction to Computer Science using Java
  • Dick Baldwin's The Essence of OOP using Java, Exception Handling Article and
  • The Handling Errors with Exceptions Lesson of Sun's Java Tutorial
  • Then, when you're ready to have some fun, take a look at this past JavaRanch conversation as well as http://c2.com/cgi/wiki?IlluminateTheMainline


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: A Question About Exception
     
    Similar Threads
    Exception Handling
    Exception Question from Dan Chisholm
    throws & throw ??
    Exceptions
    Q5 from Dan's top exam (2nd)