Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Exception

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception

    How to create the your own custoimzed Exception class?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Exception

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Here's useful info on creating your own exceptions.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception

    you can create an customized exception by extending the Exception class

    sample code for you:

    public class MyException extends Exception {

    public MyException(String message)
    {
    super(message);
    //System.out.println(message);
    }

    }




    public class ExceptionDemo {

    public void displayNumbers() throws MyException
    {
    for(int i=0;i<10;i++)
    {
    System.out.println("i= "+i);
    if(i==6)
    {
    throw new MyException("My Exception Occurred");
    }
    }
    }


    public static void main(String[] args) throws MyException {

    ExceptionDemo exceptionDemo = new ExceptionDemo();
    exceptionDemo.displayNumbers();



    }

    }

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception

    Java provides a way to define our own exceptions. It is good practice to defined the custom(user-defined) exceptions for every application. This helps in handling exceptions better and pass additional information along with exception. The hierarchy of custom exceptions can help taking appropriate actions at various module levels.
    For more information visit below link.
    http://java.meritcampus.com/t/155/Us...ned-exceptions

  5. #5

    Default Re: Exception

    1. Create a new class whose name should end with Exception like ClassNameException. This is a convention to differentiate an exception class from regular ones.
    2. Make the class extends one of the exceptions which are subtypes of the java.lang.Exception class. Generally, a custom exception class always extends directly from the Exception class.
    3. Create a constructor with a String parameter which is the detail message of the exception. In this constructor, simply call the super constructor and pass the message.

  6. #6
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 9 Times in 6 Posts

    Default Re: Exception

    Java has many built-in exception classes, such as NullPointerException and IllegalArgumentException. At times however, you might want to create your own exception class. For example, as opposed to throwing IllegalArgumentException when a 0 is detected as a divisor during a division operation, you might wish to throw a DivideByZeroException. This exception class does not exist in the Java core API, but you can create one yourself. The seven steps below will show you how to create an exception class in Java.

    First, you will create the custom exception class. Open your text editor and type in the following Java statements:
    Java Source for Create an Exception Class
    The class extends the Exception class that is defined in the Java core API (in the package is java.lang). When extending Exception you are defining a "checked" exception, i.e., an exception that must be caught or thrown. A constructor is provided that passes the message argument to the super class Exception. The Exception class supports a message property.
    Save your file as DivideByZeroException.java.
    Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.
    Compile Source for Create Exception
    Now you will create the program to test your new exception class. Open your text editor and type in the following Java statements:
    Java Source for Testing Exception Class
    Notice that the divideInt method must provide a throw clause because the method potentially throws the DivideByZeroException. To create the exception object, the program uses the throw keyword followed by the instantiation of the exception object. At runtime, the throw clause will terminate execution of the method and pass the exception to the calling method.
    Save your file as TestDivideByZeroException.java.
    Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.
    Compile Source for Test Create Exception
    Type in the command to run your program and hit Enter.
    Run for Test Create Exception
    The first call to the divideInt method is successful. The second call, using a divisor of 0, causes the DivideByZeroException to be thrown in the divideInt method. The message passed to the constructor is displayed in the output.

Similar Threads

  1. Replies: 4
    Last Post: February 20th, 2013, 10:01 AM
  2. Replies: 1
    Last Post: February 6th, 2013, 11:21 AM
  3. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  4. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  5. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM