exception handling – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Thu, 08 Apr 2021 15:51:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg exception handling – Programmerbay https://programmerbay.com 32 32 Difference between Checked and Unchecked exception in Java https://programmerbay.com/difference-between-checked-and-unchecked-exception-in-java/ https://programmerbay.com/difference-between-checked-and-unchecked-exception-in-java/#respond Fri, 09 Oct 2020 18:17:57 +0000 https://www.programmerbay.com/?p=1799 In this article, we’ll be discussing the meaning of exceptions, their type, and the difference between checked and unchecked exceptions.

An exception can be defined as any event that causes an interruption in executing a program and disrupts its normal flow. ( What happens when an exception is occurred ? )

It can be handled using an exception handler with the help Try, Catch, finally, and resources components. An effective exception handler can prevent a program to get terminated forcefully.

Exception Hierarchy

 

In Java, Runtime exception and error are together known as Unchecked exception.

An exception can be classified into two types:-

  1. Checked Exception
  2. Unchecked Exception

Checked exception vs unchecked exception

Further, “Throwable” is the super class of Error and Exception class.

The basic difference between these two types of exceptions is,  Checked exceptions are identified at compile time, whereas Unchecked exceptions are those which are not identified at compile time by the compiler.

 

Difference between Checked and Unchecked Exception in Java

CHECKED EXCEPTIONUNCHECKED EXCEPTION
An exception that triggers at compile timeAn exception that triggers at runtime
A compiler can anticipate these exceptions and can be handled effectively and efficientlyA compiler can't anticipate these exceptions. Its programmer's responsibility to deal with these exceptions
All exceptions under Exception class are referred checked exception, except RuntimeException and its subclasses, RuntimeException and Errors classes are together referred unchecked exception and not a part of Exception class
It identifies and encounters compile time exception, if it is not handledIt doesn't identify and encounter compile time exception and programmer is responsible for it
For example, FileNotFoundExceptionFor example, NullPointerException and IOError

Checked exception

  1. This exception arises due to our program or application.
  2. This exception is certain to the compiler and that’s why it is predictable and can be handled efficiently.
  3. It is recoverable.
  4.  For Example, in a program, if you try to read a file that does not exist, then the compiler would trigger an error on to the screen.
  5. These are compile-time errors.
  6. The thrown exception can be caught by catch statements.

 

Java Program to demonstrate checked exception ?

    public static void main(String[] args) {

        File file = new File("c/programmerbay");
        FileInputStream stream = new FileInputStream(file);  // Compile time error, need to enclose this line in try catch block

}

Unchecked Exception

An unchecked exception can be classified in two types :

  • Runtime Exception

  1. This exception is also caused by our program, it occurs due to logical errors or irrational use of API.
  2. The exception cannot be predicted by the compiler.
  3. It is irrecoverable.
  4.  For example, suppose, you’re trying to read a file by passing the name of the file but somehow, due to logical error, a null gets passed to the constructor, the constructor would throw a respective runtime Exception.
  5. These are run-time errors.
  6. The thrown exception can be caught and handled only if the programmer aware of the problem, otherwise it is difficult to catch. However, it is better to avoid this situation.

Java Program to demonstrate unchecked exception ?

  public static void main(String[] args) {

        int x = 5;
        int y = 0 ;
        int result = x/y;  // Exception would be occurred after executing the program at this line, ArrayIndexOutOfBoundsException
}
  • Errors

  1. This exception is not arisen by our application, instead of this, it occurs because of the lack of system resources.
  2. This cannot be anticipated by the compiler and therefore it is difficult to handle.
  3. It is irrecoverable
  4. For Example, your system’s heap memory may be got full and it runs out of the memory. resulting your program would not be executed, here, your application isn’t accounted for this, a programmer can’t do anything in this situation.
  5. These are also identified at runtime.
  6. The thrown exception cannot be handled by catch statements as these exceptions are external to your application, most of the time applications end up with termination.

 

]]>
https://programmerbay.com/difference-between-checked-and-unchecked-exception-in-java/feed/ 0
Java Exception handling and its hierarchy https://programmerbay.com/java-exception-handling-and-its-hierarchy/ https://programmerbay.com/java-exception-handling-and-its-hierarchy/#respond Sat, 14 Sep 2019 17:29:01 +0000 https://programmerbay.com/?p=5113 What is an Exception?

An exception is an event that occurs when a program fails to achieve the normal flow which leads to the termination of that program. In other orders, When a program doesn’t follow rules or constraints defined in Java language then it is said to be an exception.

What happens when an exception occurred?

new exception
  • When an exception is triggered, the method in which it has occurred creates an object, referred as exception object that contains the information of what type of exception this is and state of the program.
  • After creating an exception object, it hands over to JRE,  this stage is termed as throwing an exception.
  • Now, JRE keeps looking for an appropriate method having a block of codes from call stack (list of methods) to handle that exception called exception handler by tracing back all the way from the method where an error occurred to the appropriate exception handler.
  • Ones an exception handler is found, the exception object is thrown to that handler, this is known as catching an exception. if no handler is found for it, then the program would be terminated.
what happen is

Hierarchy of Exception

The throwable class covers entire exception handling in Java.

Exception Hierarchy

 Throwable class can be classified into two subclasses:-

Exception:  An exception which occurs when the problem is specifically triggered by our program and disrupts the normal flow of that program. For example, ArrayOutOfBound exception, I/O exception and more. In other words, an exception is often caused by a program itself. It is recoverable.

An exception can also be divided into two type:-

  • Unchecked Exception: These exceptions are those exceptions which are not identified at compile time by the compiler. For example, RuntimeException.
  • Checked Exception: Checked exceptions are those exceptions which are identified at the compile time by the compiler. For example, FileNotFound exception.

Error: An error occurs when the system in which our JRE executes has insufficient resources for our own program. For example, stack overflow. It is not recoverable.

]]>
https://programmerbay.com/java-exception-handling-and-its-hierarchy/feed/ 0