• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Question About Exception

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic