Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checked vs Unchecked exception

I've studied that: With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method.

what its mean?

according to that code there is no need to put try catch block in code, but i've seen compiler forces to put the code in try catch block.

I'm very confused what they are exactly?

like image 627
Laawanya Avatar asked Jan 09 '11 13:01

Laawanya


People also ask

What is the difference between a checked and unchecked exceptions?

A checked exception must be handled either by re-throwing or with a try catch block, a runtime isn't required to be handled. An unchecked exception is a programming error and are fatal, whereas a checked exception is an exception condition within your codes logic and can be recovered or retried from.

What is an example of a checked exception?

ClassNotFoundException, IOException, SQLException etc are the examples of the checked exceptions. I/O Exception: This Program throw I/O exception because of due FileNotFoundException is a checked exception in Java.


1 Answers

  • Checked Exceptions are useful for handling events that occur in the normal operation of a program. An example would be an IOException that is thrown when a file cannot be opened. These exceptions occur even if there is nothing wrong with the program. It is necessary, therefore, to tell the program how to handle the exception.
  • Unchecked exceptions are useful for identifying defects in the code. For instance, a NullPointerException is thrown when a value is read on a null object. Thus an Unchecked Exception represents a problem that requires a manual fix by the programmer. It is reasonable for the program to crash in order to avoid erroneous behavior, so a try-catch block is not required (but might be desirable in order to provide mitigation, such as displaying an error to the user).
like image 56
Paul Wintz Avatar answered Sep 30 '22 08:09

Paul Wintz