Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Exception

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is Check Exception & Run Time Exception?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raj,
Exceptions r of 2 types checked or unchecked. Except for RuntimeException,Error & their subclasses all exceptions r checked. Exceptions defined by Error & RuntimeException classes & their subclasses r called unchecked.
The compiler ensures that if a method or a constructor can throw a checked exception then it must either catch the exception or pass it on to its caller. Since its been chekced at compile time hence the name checked exceptions.
Where as for unchecked exceptions the compiler doesnot complain even if its not taken care of ( i.e no try-catch or throws clause). The reason for not checking it is RuntimeException can normally be avoided if the program is written correctly. Hence it should not arise in a correct program, so u r not required to handle it.
Kiran Shetty
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi raj, Kiran,
Just an FYI:


The reason for not checking it is RuntimeException can normally be avoided if the program is written correctly. Hence it should not arise in a correct program, so u r not required to handle it.


That's somewhat misleading RuntimeExcepetions are those than can occur anytime, anywhere.


JLS §11.2.1 Why Errors are Not Checked
Those unchecked exception classes which are the error classes (Error and its subclasses) are exempted from compile-time checking because they can occur at many points in the program and recovery from them is difficult or impossible. A program declaring such exceptions would be cluttered, pointlessly.
11.2.2 Why Runtime Exceptions are Not Checked
The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java programming language, having to declare such exceptions would not aid significantly in establishing the correctness of programs. Many of the operations and constructs of the Java programming language can result in runtime exceptions. The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer.
Requiring such exception classes to be declared would simply be an irritation to programmers.


Ideally, they should not occur. You can still catch and handle them in your code if you know one ie ArthmeticException will be generated.
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jane,
You r right about the why RuntimeException & Errors r not checked at compile time. Thanks for the information.
Kiran GShetty.
 
raj yagnik
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me
checked exception will check while compile time or runtime?/
and unchecked exception will check while compile time or runtime?
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Checked Exceptions are checked during Compile time.
Unchecked Exceptions are checked during Run time.
Please correct me if I am wrong.
Jane's quote from JLS is very informative.
Vanitha.
 
raj yagnik
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
u are right.
raj yagnik
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!