All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Types Of Exception Handling In Java

Last Updated : Jul 1, 2023

Types Of Exception Handling In Java

In this article we will show you the solution of types of exception handling in java, an exception is an occurrence during programme execution that disrupts normal flow of the program' instructions.

Exceptions are objects in Java that describe an unusual (error) situation that has took place in a piece of code.

They are employed to handle errors as well as other exceptional incidents in Java-coded programmes.

Exceptions in Java can occur as a result of a variety of circumstances, including incorrect data entered by the user,hardware failure,network connection failure, or even a database server that is unavailable.

Exception handling refers to the code which specifies what should be done in specific exception scenarios.

Exception as well as its subclasses are a type of Throwable that represents conditions which a reasonable implementation may wish to catch.

Checked exceptions are the class Exception as well as any subclasses that aren't also subclasses of RuntimeException.

Checked exceptions must be declared in the throws clause of a method or function Object() { [native code] } if they are thrown by the method or constructor's execution and propagate outside of the technique or function Object() { [native code] } boundary.

When it comes to exceptions, they are subclasses of a built-in Throwable class.

As shown in Figure 1, another subclass derived from of the Throwable class is Error.

This same error could be defined as an unusual condition which indicates something went wrong with the program's execution.

Java programmes do not handle these :-

1 Check exceptions :

These are exceptions that the compiler checks at compile time. If a method decides to throw a check exception,the method's caller must handle the exception as well as make known it within the throws clause.

2 Unchecked exceptions are fainal exceptions that aren't checked by the compiler.

Runtime exceptions as well as errors are among them. Unchecked exceptions are exceptions which occur during the program's execution. Runtime exceptions are thus also called runtime errors.

During the compilation process, these exceptions are typically ignored.

They really aren't checked during program compilation. For example, logical errors in programming and the use of incorrect APIs.

Step By Step Guide On Types Of Exception Handling In Java :-

import java.io.*;
Class demo{
 public static void Main(String args[]) {
   InputStream input 1 =null;
     Input 1 = New InputStream("C:/demo.txt");
     int m;
     while(( m = input1.read() ) != -1) {
            System.out.print((char)m);
     }
        input1.close();
 }
}
  1. First, we make an import function called java.io.*.
  2. The following step involves creating a class named demo.
  3. As a result, as a string argument, a public static void main has been created.
  4. then we create FileInputStream input as null.
  5. The following step involves creating the file.
  6. We then end the program.

Conclusion :-

Runtime exceptions as well as errors are among them. Unchecked exceptions are exceptions that occur during the program's execution. Runtime exceptions are thus also called runtime errors.

I hope this article on types of exception handling in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪