All TalkersCode Topics

Follow TalkersCode On Social Media

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

Throw Exception In Catch Block Java

Last Updated : Mar 11, 2024

Throw Exception In Catch Block Java

In this article we will show you the solution of throw exception in catch block java, an exception-throwing code is enclosed in a try block in Java. In order to use it, it must be used within the method.

A try block with an exception will not execute the rest of the block code if it has an exception at the stated statement.

If a try block is followed by a catch or a finally block, it is considered a Java try block. We will now discuss the idea of how to throw exceptions in the catch block in java with an example.

Step By Step Guide On Throw Exception In Catch Block Java :-

Code 1

import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class TalkersCode TryCatchExample {
    public static void main(String[] args) {
        PrintWriter pw;
        try {
            pw = new PrintWriter("tc.txt");
            pw.println("saved");
        }
 catch (FileNotFoundException e) {
            System.out.println(e);
        }
    System.out.println("File saved successfully");
    }
}

Code 2

public class TalkersCode {
    public static void main(String[] args) {
        try
        {
        int arr[]= {2,4,6,8};
        System.out.println(arr[10]);
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println(e);
        }
        System.out.println("rest of the code");
    }
}
  1. The FileNotFoundException and PrintWriter classes are imported to meet our program's requirements.
  2. An example program is created by creating a public class named "TryCatchExample" that contains the main method.
  3. Inside the main method, we create a PrintWriter object named "pw".
  4. We put the PrintWriter object inside a try-catch block.
  5. Inside the try block, we create a new file named "tc.txt" using the PrintWriter object and write "saved" to the file.
  6. In the catch block, control transfers in case an exception occurs when creating the new file.
  7. A message describing the exception will be printed inside the catch block.
  8. In the last step, we print an error-free message on the console if no exception is thrown.
  9. An exception message or a message indicating that the file has been saved successfully is output by the entire program.

Conclusion :-

As a result, we have successfully learned how to throw exceptions in the catch block in Java with an example.

The purpose of Exception Handling is to prevent abnormal circumstances from terminating the program.

In addition, it is used as a means of logging and maintaining the flow of our program.

When an exception occurs, it is handled by the catch block. The try blocks contain code that may throw an exception, and the catch blocks contain code that addresses the exception.

When an exception unhandled by a program terminates, the program's final block is always executed.

I hope this article on throw exception in catch block java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪