Checked vs Unchecked exception

2019-01-06 20:03发布

问题:

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?

回答1:

Unchecked exceptions are those that extend RuntimeException class. Compiler will never force you to catch such exception or force you to declare it in the method using throws keyword. All other exception types (that do not extend RuntimeException) are checked and therefore must be declared to be thrown and/or catched.

Checked exceptions are used when you want the caller of your method (i.e the user of your API) to explicitly handle the exceptional case in your API. Checked exceptions are declared when you believe the call will be able to do something meaningful with that exceptional case, like retrying the call, rolling changes back or converting it into some user-readable error message.

If you believe that there is nothing useful the call can do about the exception (especially when it represents a bug, or a wrong usage of your API), then the exception should be unchecked. Also, an API with too many checked exceptions can be annoying to program with (e.g. try using java reflection API=)



回答2:

What is your question exactly? Compilers shouldn't (and won't) enforce you to try/catch unchecked exceptions, that would go against exactly what they are.

The general idea is that checked exceptions are something you may be able to foresee but may be based on input that is out of your control and that you have to deal with. Unchecked exceptions will usually represent bugs in your program.

There's a number of people that think checked exceptions are a mistake in the Java platform and they only use them very sparingly or not at all. You can read more about this debate by searching google.



回答3:

It is because,

  1. Unchecked Exceptions are not a result of programmer's fault. Instead they are the serious consequences where we(programmer) aren't expected to do much with it.
  2. In case of Checked Exception, it is an exception generated because of the programmer's fault & often can be resolved by programmer itself.

Check the following links :

Why RunTime Exceptions are unchecked ?
Checked vs Unchecked Exception ?



回答4:

  • 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).