All TalkersCode Topics

Follow TalkersCode On Social Media

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

User Defined Exception Handling In Java

Last Updated : Mar 11, 2024

User Defined Exception Handling In Java

In this article we will show you the solution of user defined exception handling in java, the keyword "throw" is used to construct and throw a particular exception known as the Java consumer exception.

It is doable by expanding the "Exception" class. An exception is an issue that happens while the system is executing.

The object-oriented programming language in Java provides a strong method to handle these exceptions.

Custom exception classes can be created in Java, and each one provides a different exception class implementation. These exceptions are referred to as consumer exceptions or customised exceptions.

An erroneous occurrence that can occur during in the execution of the program and obstruct its usual flow is referred to as an exception.

Java Exception Handling, a powerful and object-oriented method of handling exception scenarios, is provided by Java.

Java exceptions can happen in a variety of circumstances, such as when a user enters incorrect data, when hardware fails, when a network connection breaks, or when a database server is unavailable.

Exception handling refers to the code that describes what to do in particular exception conditions.

Only runtime faults are handled by the Java Exception handling framework.

The developer creating the code must correct compile-time defects otherwise the application won't run.

Step By Step Guide On User Defined Exception Handling In Java :-

import java.io.FileNotFoundException;
import java.io.IOException;
public class TalkersCode{
            public static void main(String[] args) throws FileNotFoundException{
                           try {
                                          demoException(-10);
                                          demoException(-20);
                           } catch(FileNotFoundException e) {
                                          e.printStackTrace();
                           } catch(IOException e) {
                                          e.printStackTrace();
                           } finally {
                                          System.out.println("TalkersCode exception");
                           }
                           testException(20);
            }
            public static void testException(int i) throws FileNotFoundException, IOException {
                           if (i < 0) {
                                          FileNotFoundException myException = new FileNotFoundException("Negative Integer " + i);
                                          throw myException;
                           } else if (i > 10) {
                                          throw new IOException("Only supported for index 0 to 20");
                           }
            }
}
  1. The testException() method throws exceptions by using the throw keyword. In order to notify the caller of the potential exception kinds, the tosses keyword is included in the method signature.
  2. The try-catch block is used in the main() method to handle exceptions.
  3. The finally block is then completed, and the testException(-20) is never executed as a result of the exception.
  4. The printStackTrace() function of the Exception class is helpful for debugging.

Conclusion :-

Java exceptions can happen in a variety of circumstances, such as when a user enters incorrect data, when hardware fails, when a network connection breaks, or when a database server is unavailable.

Exception handling refers to the code that describes what to do in particular exception conditions.

Only runtime faults are handled by the Java Exception handling framework.

The developer creating the code must correct compile-time defects otherwise the application won't run.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪