All TalkersCode Topics

Follow TalkersCode On Social Media

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

Java Throw Exception With Message

Last Updated : Aug 7, 2023

Java Throw Exception With Message

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();
   }
}
  1. In the code, there is a class named "TalkersCode" and a constructor method that takes two parameters: "name" and "age".
  2. 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.
  3. This exception instance is created and given a message called "AgeDoesnotMatchException".
  4. An exception can be thrown by using the keyword "throw".
  5. In order to catch the exception thrown by the constructor, it is enclosed in a try-catch block.
  6. By using the "printStackTrace()" method, you can print out the exception instance.
  7. A value is assigned to name and age during the constructor phase, and the values are stored in instance variables.
  8. There is also a method for displaying the name and age of a student in the class called "display."
  9. It defines a main method that creates a Scanner object that can be used to gather data from the user.
  10. In addition to the student's name and age, the user must enter their account information.
  11. Upon entering the name and age, a new object "obj" will be created within the "Student" class.
  12. 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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪