Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

Exception Handling in Java : Checked and Unchecked Exceptions

Errors and unexpected situations are an integral part of programming. It rarely happens that programs are error free. It is the responsibility of the programmer to identify all possible errors and correct them so that the program runs. There are different types pf errors that can occur in a program. Syntax errors occur when the rules and the syntax of the language is not followed. For e.g. if we forget to end a statement with semicolon or we misspell a keyword. Syntax errors are detected and reported by the compiler. Logical errors occur when we make a mistake in the program logic i.e,. algorithm. For e.g. instead of addition, we perform subtraction. In such a case, the program will run but will not give the correct output. Removal of logical errors is called debugging which should be done by the programmer.
Another kind of errors are called exceptions which are run-time errors which occur when the program is running. Now here we will look after exceptions and exception handling in detail.

Dealing with Errors
Since errors are an intrinsic part of programming, we cannot ignore them but we have to deal with errors. When a runtime error occurs in a program, there are two things that should ideally take place.
  1. The program should return to a stable state and allow the user to take further action.
  2. If the program has to be terminated, it should five the user to save all work before termination.
The above may not happen automatically in the program. For this purpose, the programmer must understand the various causes of errors, anticipate what kind of errors can occur in the program so that the program does not terminate abnormally when some error occurs.

Causes of errors :
  1. User Input Error – These errors occur in input values given to a program. These include invalid out of range values given to variables, values of wrong types, invalid paths etc.
  2. Device Error – These errors are related to the hardware devices which the program uses. For e.g., a network connection may be unavailable, the printer may fail, the removable disk may be malfunction while file I/O etc.
  3. Physical Limitations – These errors are caused due to limitations on available the printer, stack space for recursion , limited connection bandwidth etc.
  4. Code Errors – These errors are caused due to the code itself. These include violation of constraints, invalid method calls, illegal assignments, undefined class, exceeding array limits etc.
Any of these can occur in a program. An elegant way to handling these occurences is by theuse of an Exception handling mechanism which is discussed in the next section.

An exception is an abnormal condition that arises in a code at run time. In other words an exception is a runtime error. Java has an inbuilt exception handling mechanism.
An exception in Java is an object trhat describes the occurrence of some exceptional or unexpected condition during execution. It encapsulates the information of the error so that further action can be taken. This is what happens when an exception occurs:
  1. When an exception arises, an object representing that exception is exception itself.
  2. The method in which the error occurred may choose to handle the exception itself.
  3. If the method can’t handle the exception, it throws this exception object to the method which called it.
  4. The exception is caught and processed by some method or finally by the default java exception handler.
Exception Classes
There are several predefined execution classes defined in the java.lang package . In the java programming language, an exception object is always an instance of a class derived from Throwable.
All exception classes extend Throwable. From the Throwable class, there are two separate hierarchies: Error and Exception.

Error: These classes are related to internal errors and resources exhaustion during runtime. The user has to control over such situations and hence, these types cannot be handled by the program. The java runtime system takes default action when this type of situation occurs.

Exception: The classes belonging to this hierarchyrepresents exceptions that a program would want to be made aware of during execution. There are two brancheshere: exceptions of the type RuntimeException and those that do not inherit from RuntimeExc eption. RuntimeException represents many common programming errors that occur runtime.

Checked and UncheckedExceptions
Exceptions can be classified into 2 types : Checked and Unchecked.

Checked Exceptions: Except for RuntimeException, Error, and their subclasses, all other exceptions are called checked exceptions. It means that it is compulsary for the user to check i.e, to handle an exception. If a method throws a checked exception then the method must take the responsibilty to deal with it. The method must either catch the exception and take appropriate action, or pass the execution on to its caller.
Examples : IOException, ClassNotFoundException, InterruptedException, CloneNotSupportException

Unchecked Exception: Exception defined by Error and RuntimeException classes and their subclasses are known as unchecked exceptions. It means that it is not mandatory for a method to deal with such kinds of exceptions, The compiler doesn’t check if a method handles or throws this exception. Such exceptions are either irrecoverable and the program should not attempt to deal with them or they cannot be treatedas exceptions.
Examples- NullPointerException, ArrayIndexOutofBoundsException,
ArithematicException, NumberFormatException etc

© 2021 Learn Java by Examples Template by Hubberspot