• 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

Exceptions

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why the above code compiles fine and why the one below gives compiler error ?

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lavjeet,

Exceptions are basically classified as: Checked Exceptions and Unchecked Exceptions.

Checked Exceptions may or may not be user-defined and, as such, need to be handled by your code.

Unchecked Exceptions are basically thrown and handled by the complier. You may or may not handle these.

Your first program:
You are catching Exception e. In other words, you are catching any of the RunTimeExceptions - Unchecked Exceptions - that are likely to occur. Since you are not expected to handle these (but you may) your code works fine.

Your second program:
You are catching Exception1 e, which is never thrown in your code. Realize that Exception1 is actually a checked exception which must be thrown *AND* handled by your code.

Here is one way to get it working:




Regards,
Saket
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic