All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Throw Exception In Java

Last Updated : Mar 11, 2024

How To Throw Exception In Java

In this article we will show you the solution of how to throw exception in java, exceptions in Java allow us to write good quality codes, as errors are checked at compile time rather than runtime.

Additionally, we can create custom exceptions to assist in code recovery and debugging.

Explicitly throwing an exception in Java is achieved by using the throw keyword.

In this case, we specify the exception object that should be thrown. Error messages are associated with exceptions and provide details on the error.

User inputs, server errors, and many other factors may cause such exceptions. We will now discuss the idea of how to throw exception in java with an example.

Step By Step Guide On How To Throw Exception In Java :-

/ class represents user-defined exception
class TalkersCode UserDefinedException extends Exception
    public UserDefinedException(String str)
        // Calling constructor of parent Exception
// Class that uses above MyException
public class TC
    public static void main(String args[])
            // throw an object of user defined exception
            throw new UserDefinedException("This is user-defined exception");
        catch (UserDefinedException ude)
            System.out.println("Caught the exception");
            // Print the message from MyException object
  1. In order to make the "UserDefinedException" class a user-defined exception, the code defines a class named "UserDefinedException", which extends "Exception".
  2. A String parameter called "str" is passed to the constructor of "UserDefinedException" to indicate the message to display when the exception is thrown.
  3. Using the "super" keyword, the constructor calls the constructor of the parent class "Exception" and passes in the "str" parameter.
  4. An exception object is then defined to use the user-defined exception as part of the "TC" class.
  5. During the execution of the program, the "main" method will be called.
  6. Using the "throw" keyword, an object of the "UserDefinedException" class is thrown in the "try" block with the message "This is a user-defined exception.".
  7. Once the exception is caught, the "catch" block will be executed. In this instance, the "UserDefinedException" class is the exception caught.
  8. By calling "getMessage" inside the 'catch' block, the message associated with the exception will be displayed.
  9. In the catch block, the program is terminated when it has completed its execution.

Conclusion :-

As a result, we have successfully learned how to throw exception in java with an example.

An exception handling process is a way to deal with errors or exceptions during runtime. You can handle exceptions and errors in Java using the try-catch, throw, and finally keywords/statements.

Finally is also used with the try statement or with the try-catch statement, but the try-catch and the finally are used together.

I hope this article on how to throw exception 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 🡪