Newsletter

How to Create User Defined Exception in Java

Core Java » on Sep 28, 2012 { 16 Comments } By Sivateja

Apart from existing Exceptions in java, we can create our own Exceptions (User defined exceptions in java), we will how to..

Required files

  • Client.java [ My business logic will goes here ]
  • MyOwnExceptionClass.java  [ This is our own Exception class ]

Note: Our user defined class must extend from Exception class, and we must override the toString() method to display our exception message.

Client.java

package java4s;

public class Client {
public static void main(String[] args)throws Exception
{
   int price = -120;

   if(price < 0)
      throw new MyOwnExceptionClass(price);
   else
      System.out.println("Your age is :"+price);
   }
}

MyOwnExceptionClass.java

package java4s;

public class MyOwnExceptionClass extends Exception {

    private int price;

    public MyOwnExceptionClass(int price){
        this.price = price;
    }

    public String toString(){
        return "Price should not be in negative, you are entered" +price;
    }

}

Output:

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

16 Responses to “How to Create User Defined Exception in Java”
  1. suresh says:

    Hi,
    Can’t we like this, instead of overriding toString() method. can’t we call super(msg) in constructor.

    Thanks,
    Suresh.p

  2. Java4s says:

    @Suresh

    Yeah you can do..
    Changes:

    In Client.java – Line number 9, Write some description about your Exception.
    In MyOwnExceptionClass.java – Fetch the previous message through constructor and at Line number 8 write super(msg).

    Note: Ensure super(–) must be the first statement in the constructor always 🙂

    That’s it.

  3. u.balu99 says:

    thank u sir………..

  4. Hari says:

    what is the boilerplate issue in Hibernate ?how to avoid it?

  5. sonu kumar says:

    What is the difference between String, StringTokenizer and StringBuffer. complete explanation with an appropriate example..
    please make me aware as soon as possible with good clarity.

  6. what is the difference b/t user defined exception & default exception…

  7. srinivas says:

    what is the differences between ‘throws’ and ‘throw’?

  8. this very good explanation

  9. Blessed by S&M says:

    Difference between throws and throw:

    throws keyword is used to give a list of probable Exception classes which will handle the exception whereas throw keyword is used to specify a particular exception class.

  10. Anand Shukla says:

    Hello Sir,

    I have two questions :
    1)If we overload a method and we pass String in one method and Object in another So while calling if we pass ‘null’ then which method will be called and why?
    2)If we overload a method and we pass String in one method and Integer in another So while calling if we pass ‘null’ then which method will be called and why?

    Thanks Sir.

  11. Deepak says:

    Please explain me…..what is the benefit of defining a User Defined Checked Exception ? I mean If we look into the real life project..we can not deploy our build without resolving all compile time exception.So in this scenario , User defined Runtime exception can be justified…but how can we benefited from creating User Defined Checked Exception ?

  12. Mounika Ch says:

    Thanks for the info 🙂

  13. Surmeet says:

    When running the above example, it is giving the desired output but giving a warning as well:

    The serializable class MyOwnExceptionClass does not declare a static final serialVersionUID field of type long

    why so? can you please explain.

  14. stephen says:

    The Exception message mentioned in class and the output printed in console has some minor difference. (check at the word “should” ).

  15. manisha says:

    while executing this program i am getting like public type MyOwnExceptionClass must be defined in its own.
    Could you please explain for this

  16. snehal desai says:

    thanks sir so many questions are clear from that

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.