Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Exception Types

 
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
Please help me.

Why Java given 2 types of exception(checked and uncheckd) and how to decide which type of exception we should to use and where?
Suppose i have a project where i need to create multiple user-defined exception, so how i will decide that user-defined exception should be checked or unchecked?
give me some example.

Thanks in advance.

[Edit - title - MB]
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please SearchFirst. This question has come up (and will come up) many, many times.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The page The Catch or Specify Requirement from Oracle's Java Tutorials explains the three kinds of exceptions and explains when you're supposed to use which kind of exception.
 
Yatish Sonkeshariya
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ROB,

I already search for this topic but didn't got any answer, if you know link of that post could you please post that.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They serve two different purposes. Checked exceptions indicate that something went wrong, but it's not necessarily a bug or a problem with the JVM internals, so it might be something the application can handle. Unchecked exceptions indicate the opposite--programming errors or problems internal to the JVM, that the application shouldn't try to handle.

It makes sense to declare the things that can go wrong that the caller might be expected to handle (checked exceptions), and it makes sense to not have to list all the possible bugs or JVM internal problems that could go wrong. So, two different families of exception.
 
Rob Spoor
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Unchecked exceptions indicate the opposite--programming errors or problems internal to the JVM, that the application shouldn't try to handle.


Actually, Unchecked exceptions (RuntimeException and its sub classes) should not indicate problems internal to the JVM. That's where java.lang.Error and its sub classes are for. Of course programming errors in the core API should still cause runtime exceptions.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:

Jeff Verdegan wrote:Unchecked exceptions indicate the opposite--programming errors or problems internal to the JVM, that the application shouldn't try to handle.


Actually, Unchecked exceptions (RuntimeException and its sub classes) should not indicate problems internal to the JVM. That's where java.lang.Error and its sub classes are for. Of course programming errors in the core API should still cause runtime exceptions.



"Unchecked exceptions" includes RuntimeException and its descendants as well as Error and its descendants. (JLS 11.2: http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html#11.2) Note the use of the general term "exception" with a lowercase "e", which describes the entire Throwable hierarchy, as opposed to the Exception class name.

So that entire "family"--those two subtrees--is, as I stated, for programming errors (in our code) and problems in the JVM, with the breakdown, of course, as you described.
 
Rob Spoor
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected.
    Bookmark Topic Watch Topic
  • New Topic