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

A Question About Exception

 
ravel chen
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    Consider Paul's rocket mass heater.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic