Java Throw Exception With Message
Last Updated : Mar 11, 2024
In this article we will show you the solution of java throw exception with message, the Exception constructor could be provided with a more detailed exception message and the underlying caught exception could be passed to it.
There are, however, some situations where we may need more scope for custom exception attributes than the default Exception class provides.
Consider a case where your code interfaces with some third-party code or a driver.
It is possible for this third-party code to produce an error code when an error occurs.
Our application can be enhanced, if required, by creating a custom exception class that facilitates passing in error codes as enum, and using these error codes to perform additional actions.
We will now discuss the idea of how to throw an exception with a message in Java with an example.
Step By Step Guide On Java Throw Exception With Message :-
public class TalkersCode Throw Exception{ private String name; private int age; public Student(String name, int age){ try { if (age<18||age>25) { String msg = "Age is not between 18 and 25"; AgeDoesnotMatchException ex = new AgeDoesnotMatchException(msg); throw ex; } }catch(AgeDoesnotMatchException e) { e.printStackTrace(); } this.name = name; this.age = age; } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static void main(String args[]) { Scanner sc= new Scanner(System.in); System.out.println("Enter the name of the Student: "); String name = sc.next(); System.out.println("Enter the age of the Student): "); int age = sc.nextInt(); Student obj = new Student(name, age); obj.display(); } }
- In the code, there is a class named "TalkersCode" and a constructor method that takes two parameters: "name" and "age".
- An age check is performed within the constructor to determine if it falls between 18 to 25 years old (inclusive). The exception is thrown if the value does not fall within the range.
- This exception instance is created and given a message called "AgeDoesnotMatchException".
- An exception can be thrown by using the keyword "throw".
- In order to catch the exception thrown by the constructor, it is enclosed in a try-catch block.
- By using the "printStackTrace()" method, you can print out the exception instance.
- A value is assigned to name and age during the constructor phase, and the values are stored in instance variables.
- There is also a method for displaying the name and age of a student in the class called "display."
- It defines a main method that creates a Scanner object that can be used to gather data from the user.
- In addition to the student's name and age, the user must enter their account information.
- Upon entering the name and age, a new object "obj" will be created within the "Student" class.
- Displaying the student's name and age on the console is done by calling the "display" method on "obj".
Conclusion :-
As a result, we have successfully learned how to throw an exception with a message in java with an example.
This article provided a detailed explanation of the throw keyword in Java and how to throw exceptions.
In order to increase the quality of our code, we should use throw while handling exceptions.
I hope this article on java throw exception with message helps you and the steps and method mentioned above are easy to follow and implement.