Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding exceptions  RSS feed

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know very well the concept of checked and unchecked exceptions.But,a question was asked in an interview that when should we go for checked exceptions and when should we go for unchecked exceptions.I am unable to get the answer.Please help.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as i know,if we want caller to handle exception,we go for checked else for unchecked.Please tell me if there is really some condition which can decide whether we should go for checked or unchecked exceptions.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be asking quite a lot of very similar questions about exceptions . . .

I presume you are familiar with the Java Tutorial about Exceptions? Look for the link about "controversy" on the left.
I presume you realise that the number of different opinions about Java Exceptions obtained is exactly equal to the number of people asked?

The official answer is to avoid unchecked exceptions. Bruce Eckel (Thinking in Java 4th edition) suggests catching checked exceptions and wrapping them in RuntimeExceptions, which is different from what he said in the 3rd edition.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is true - somebody let me know if not - Checked exceptions were a new concept in Java. Earlier languages had gotten along on fine with only unchecked exceptions. The Java crew must have been trying to solve some problem they had run across in other languages. Whether the problem needed solving and how well they did is all up for debate.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, my opinion is that if there is a reason to code an exception, then there must be a reason and that reason needs to be delt with - even if controversy arises.

The ultimate root of this question is "Except if the exception is an exceptional exception" -> which I told my industrial engineer and I could read his face that what is going on here is lack of experience, discipline and skill. I think our advice to the beginner coder is that the definition of what constitutes an exception is a designe matter for the coder to determine.

If coder throws an un-checked exception, then coder has made the determination that the main method should throw. Generally not a good design practice but it is the coder that has to answer that question, not the designers of the language.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
I think this is true - somebody let me know if not - Checked exceptions were a new concept in Java. Earlier languages had gotten along on fine with only unchecked exceptions. The Java crew must have been trying to solve some problem they had run across in other languages. Whether the problem needed solving and how well they did is all up for debate.


They were new-ish when Java was designed, but not a Java invention; C++ had exception specifications already, for example. In that language, it isn't the type of the thing you throw that determines checked-ness, but whether or not you declare it. If you declare that a method throws X, then X is effectively checked. After some experimentation, I think most folks simply didn't use the specifications, as they caused too much pain. Don't know what the current state of the art is, as I've been away from C++ for some years now.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So,Looking at the suggestions ,can i conclude that there is no such thing to decide whether we should go for checked or unchecked exceptions
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficult to give a single answer. That is why that is a good interview question; it shows whether you have thought about exceptions.
Simple suggestion which will work in at least 0.0000000000000001% of all conceivable instances:
  • Except for a few types, eg NumberFormatException, an unchecked Exception represents a programming error.
  • Declare "throws" NumberFormat and similar exceptions once and once only.
  • Declare "throws" checked Exceptions once and once only.
  • Throw an IllegalArgumentException if there is a precondition violation.
  • Put some sort of active handling in the next method for every thrown exception.
  • Now, if you get an unchecked Exception, you can go back and correct the code where it occurred.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!