• 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

Dan's Study Guide Exams - exceptions

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(The following examples are derived from Dan�s Study Guide Exams for chapter 5.)
In the first example, how do you know the compiler will report two errors, instead of stopping after the first one?

In the above example, two compiler error occur
: unreported exception java.lang.ClassNotFoundException;

In the above example, only one compiler error occurs
: unreported exception AException; must be caught or declared to be thrown
although there is potentially another compiler error
: exception BException is never thrown in body of corresponding try statement
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
marlene ,
in the first example, the case statements fall through (without break) so it throws both the checked exceptions....
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For purposes of the exam, you won't have to know how many compiler errors might be thrown on a given piece of code - you will only be required to recognize is any compiler errors will be thrown.
Frankly, I'd say that it's impossible to know how many errors will be thrown without knowing the inner workings of the compiler, which is well beyond the scope of this exam.
Corey
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene,
In the first example...let me clarify.. at compile time all uncaught checked exceptions will be reported as compile errors
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
marlene,
In the 2nd example the compiler reports uncaught exception because method m() does not explicitly throw AException.... Hence the compile error
If u changed m() to m() throws AException{...} it will compile fine..

The compiler needs to check and be sure that checked exceptions are caught or declared to be thrown... (the only exclusion to this rule is RuntimeException or Errors)... As long as this rule is adhered to it has no problem....
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Preeti and Corey.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that the compiler knows the class hierarchy. In other words, if you modify the code in m() to throw BException and catch AException, then the code compile fine without errors.
 
reply
    Bookmark Topic Watch Topic
  • New Topic