Laawanya
Laawanya

Reputation: 249

Checked vs Unchecked exception

What does the following quote mean?

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.

According to that quotation, there is no need to use try-catch blocks, but I've seen the compiler require some code to be placed in try-catch blocks.

Why do some exceptions require try-catch blocks and others don't?

Upvotes: 16

Views: 17570

Answers (8)

Chandra Sekhar Jena
Chandra Sekhar Jena

Reputation: 1

CHECKED EXCEPTION:

Basically Checked Exception are the exception, which actually occurs when we are dealing with any resources, like File,Database,Network etc.So,during loading another class for any purpose like when we use Class.forName(String ClassName) for dynamically load the Driver class at runtime during connection of database or any other purpose we are dealing with another class , which acts as a resource for our class and another one is when we use File for write or read some data the file also is a resource so basically in my observation when we handling with resources then they throws checked exception which we need to catch explicitly by try-catch block or after method declaration we can write with throws which will be handle by JVM's default exception handler. Examples Of Checked Exception:

IOException (we are depending upon System Keyboard OR Files ) FileNotFoundException(We are depending upon the file) InterruptedException (Thread related problem) ClassNotFoundException (class related problem) SQLException (SQL related or database related problem) CloneNotSupportedException (Object is the resourse) EOFException(We are depending upon the file)

And , another thing all the checked exception are directly subclass of either java.lang.Exception or java.lang.Throwable

Unchecked Exception

Unchecked Exception are sub-class of RuntimeException class.this is the way to identify unchecked exception.An Unchecked exception is rare and most of the the time these are not linked with any resource . These are directly handled by default exception handler by jvm . it is upto the programmer to handle the exception. if we do not handle exception application will terminate abnormally throwing that perticular exception. if we handle it then program throws exception, we can provide user-friendly message to user related to exception.because our client might a non technical person if he is dividing a number by 0 and we provide him java.lang.ArithmeticException may be he could not understand ,by providing user-friendly message "Like Dont divide any number by 0" it would be better approach.so it is reccomended to catch every possible exception(whether checked or unchecked ) in our application to provide user-friendly message and a normal termination.

Upvotes: 0

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

It is because,

  1. Checked 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 Unchecked 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 ?

Upvotes: 3

Rahul Gupta
Rahul Gupta

Reputation: 1129

in simple words,

  • checked exceptions are those which can be and should be handled by your code(therefore compiler forces you to handle them)
  • unchecked exceptions are those which lie beyond programmer's control(therefore compiler doesn't forces you to handle them)

use the same rule even while creating your custom exceptions.

Upvotes: 0

Vidur Agarwal
Vidur Agarwal

Reputation: 11

All Exceptions are part of run time and not compile time. There are two kinds of exceptions checked exceptions and unchecked exceptions. Examples of checked exceptions are IO Exceptions, ClassNotFound Exception and examples of unchecked exceptions are runtime exceptions. In the case of checked exceptions, error or warning message comes at compile time so that the user will take care of it at runtime by using throws keyword, which is used to throw exceptions to the default catch mechanism system. But in case of unchecked exceptions warning is not there at compile time.

Upvotes: 1

**Checked Exceptions

Exceptions which are to be checked or handled or should be taken care during the time of writing the code are called as checked exceptions. For eg: 1. we have FileNotFoundException -->which will be occured when we are writing some code related to file classes. There will e defenetly posibility of non existence of file. In such case in order to handle them , we are forced to handle those exception for sure. 2. one more example is ParseException ,which will be occured when we are dealing with date functions.

UnChecked Exceptions

These are the exceptions that are optional to be handled during the time of coding. Its up to us whether we handle them or not. In case if we fail to handle them, There is a chance of getting runtime errors during the exceution. For eg: We have something called NullPointerException,ArithemeticException,NosSuchElementFoundException and so on. These are like optional things we dont even have to handle them. More over even jvm or compiler will not recommend us to handle them.**

Upvotes: 0

Paul Wintz
Paul Wintz

Reputation: 2743

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

Upvotes: 12

rodion
rodion

Reputation: 15029

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=)

Upvotes: 30

Sebastiaan van den Broek
Sebastiaan van den Broek

Reputation: 6331

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.

Upvotes: 5

Related Questions