• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unchecked exception

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unchecked exception is a bug, "cannot be reasonably recovered from at run time". I don't understand this statement.

For example, NullPointerException is runtime exception. Say user is required to input his name to login, but he didn't.

Coding is below. Why this is not recovered ? we can try-catch that NullPointerException, throws a checked exception, popup a warning dialog, say "you didn't enter name". You see, system is still stable, still running well, even don't need recover.

Thanks.

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in that case you should be writing specific validation code to check that the name has been entered. You shouldn't be assuming that it's there and waiting for some point where an exception is thrown. Generally speaking, unchecked exceptions are down to a bug in the code.

(In that particular example you probably wouldn't get a null value. You'd get an empty string. Which is just as bad as far as the application is concerned, but wouldn't cause an exception).
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward Chen wrote:Unchecked exception is a bug, "cannot be reasonably recovered from at run time". I don't understand this statement.

I would disagree with the wording that the author of that statement used. An error is something that cannot be reasonably recovered from. An unchecked exception is simply an exception that you are not required to explicitly catch or throw up the stack. While they may be things that shouldn't normally occur in a well designed program, I think the real reason for having them unchecked is simply that it would be incredibly tedious and clutter up the code--you don't want to have to put every division operation in a try-catch block because of the possibility of zero divisor. On the other hand, that doesn't mean that you might not choose to use them in certain circumstances. There might be situations where you prefer to put an arithmetic calculation in a try-catch block rather than adding code to check if any divisors are zero. That's just a design decision.
 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would disagree with the wording that the author of that statement used. An error is something that cannot be reasonably recovered from. An unchecked exception is simply an exception that you are not required to explicitly catch or throw up the stack. While they may be things that shouldn't normally occur in a well designed program, I think the real reason for having them unchecked is simply that it would be incredibly tedious and clutter up the code--you don't want to have to put every division operation in a try-catch block because of the possibility of zero divisor. On the other hand, that doesn't mean that you might not choose to use them in certain circumstances. There might be situations where you prefer to put an arithmetic calculation in a try-catch block rather than adding code to check if any divisors are zero. That's just a design decision.


agreed
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some places in the Java Tutorials where the authors have obviously been slack in their use of the English language, and that is one of them.
Also, on this page, they call && "logical AND" and here, they call it "conditional AND". ["Conditional" is correct.]
 
Stephan van Hulst
Bartender
Pie
Posts: 5885
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree Kurt. While it's true that unchecked exceptions are helpful in keeping code clutter free, most of the time they signify bugs.

What are you going to do if your user wants to divide by zero, and you catch the exception? Are you going to make your program logic depend on this? This is a grave misstep, in my opinion.
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I disagree Kurt. While it's true that unchecked exceptions are helpful in keeping code clutter free, most of the time they signify bugs.

Stephan, I don't think we're really in disagreement about this. Unchecked exceptions are things that the programmer generally has control over, like making sure that arguments passed to methods are valid, and divisors aren't zero, etc., while checked exceptions deal with things the programmer can't explicitly control, like whether an external file exists. In a well-designed program unchecked exceptions shouldn't generally happen. The only point I'm making is that an unchecked exception is only a bug if: 1) it occurs, and 2) it's uncaught. While in most circumstances one would want to ensure that the exception doesn't occur in the first place, the programmer might, as a design choice, allow an unchecked exception to occur and then catch it and deal with it.

To use the division by zero example, let's suppose I'm writing a command-line calculator program. I could, as I parse and execute the input, check for zero divisors as I go along. But I might also choose to put the whole routine inside a try-catch clause. Either way, I can have the program display a message to the user that they entered an illegal expression.
 
Stephan van Hulst
Bartender
Pie
Posts: 5885
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kurt Van Etten wrote:The only point I'm making is that an unchecked exception is only a bug if: 1) it occurs, and 2) it's uncaught. While in most circumstances one would want to ensure that the exception doesn't occur in the first place, the programmer might, as a design choice, allow an unchecked exception to occur and then catch it and deal with it.

[...]

But I might also choose to put the whole routine inside a try-catch clause. Either way, I can have the program display a message to the user that they entered an illegal expression.


This is what I disagree with. You say an unchecked exception is a bug if it occurs, and if it's uncaught. I say an unchecked exception is a bug if it occurs period. I think that in a well designed program, you should never catch an unchecked exception. Putting the division routine in a try-catch clause is a poor design choice, in my humble opinion. Always use a conditional statement. The only time it is valid to catch an unchecked exception, is when it's too specific or too broad to let it propagate up the stack. In this case, you wrap it in a more general or more specific unchecked exception, and rethrow it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:. . . I say an unchecked exception is a bug if it occurs period. . . .
Agre, but some time ago somebody pointed out there are a few xceptions to that rule, eg NumberFormatException.


This sort of discussion makes a website so very worthwhile.
 
Stephan van Hulst
Bartender
Pie
Posts: 5885
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will concede that in cases like that, catching an unchecked exception may be a valid option, from a pragmatic point of view.

However, it still indicates bad design decisions; for example, Integer is very deficient for not providing a String testing method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote: . . . bad design decisions . . .
Good point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic