• 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

why Checked & Unchecked

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
helo friends.
I got one doubt regarding Exception.
Why There is Checked and Unchecked Exception. What is Special about Checked Exp.
Why java Compiler tell you force fully to try and catch when function throws Checked Exception .
wating for positive answer.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checked and Unchecked refers to things that the compiler CAN and CANNOT check for at compilation time. Unchecked exceptions inherit from the class RuntimeExceptions. So:
- Checked. These are the error-conditions that a program MUST pick up and handle, and it is an error **AT COMPILE TIME** if they're not. This includes things like File-Not-Found (in IOException). Clearly, the compiler knows that this condition MAY happen, so it demands that the condition be caught.
- Unchecked. These are things that the compiler cannot check for at compile time (ie. it would be too much of a performance overhead), but can happen at run-time. eg. a = b/c; is fine at compile-time, but may fire an ArithmeticException at runtime should c == 0.

In terms of coding, checked exceptions MUST be explicitly coded for in try/catch blocks or in "throws" clauses at the method declaration. Failure to do so results in a compiler error.
Unchecked exceptions are like ghosts: They may occur anywhere, and as soon as they do they follow the standard exception-handling rules. However, since most programs only have 'catch' blocks for checked exceptions, unchecked exceptions perform the "finally" blocks and abort the current method until they are either caught, or they abort the program.
Note that you CAN have catch blocks for unchecked exceptions (eg. try { .... } catch (ArithmeticException e) { .... }; ), and unchecked exceptions WILL be caught by a "catch (Exception e)" (since RuntimeException inherits from Exception). The only difference is what the compiler will INSIST you check for.
Hope this helps
 
vishwas bhatt
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks's Rob. for such nice Explanation.
it will help me lot. I got one more problem reagrding this.
may be you fell it's crazy.but i want to know why jvm need to catogrized exception in sunc way. why it's checking some expection at compile time and some at run time.
is there any answer. if you fell it's crazy then also give me suggestion.
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic