How to create custom exception in Java?

In this tutorial you will learn How to create custom exception in Java?

How to create custom exception in Java?

In this tutorial you will learn How to create custom exception in Java?

Making Custom (User Defined) Exceptions

How to create custom exception in Java?

In this tutorial we are going to create our own exception class and you will learn the steps to create custom exception class. Custom exceptions are used by Java developers to create own exception class in their application.

For example if you are developing Bank Account application and if the account does not exists then for this you can create BankNotFoundException class. In this example we have explained you to create own exception class and then use in the program. The Exception class defined by developer is called Custom or User Defined Exception.  To define a custom exception class you have to extend the class with Exception class. Lets start developing our custom exception class.

In Java API all exception classes have two type of constructor. First is called default constructor  that doesn't accept any arguments. Another constructor accepts a string argument that provides the additional information about the exception. So in that way  the Custom exception behaves like the rest of the exception classes in Java API.

There are two primary use cases for a custom exception. 

  •  your code can simply throw the custom exception when something goes wrong. 
  • You can wrap an exception that  provides extra information by adding your own message.

The code of a Custom exception:

public  class ExceptionClassName extends Exception

{

   public ExceptionClassName(){ }

  public ExceptionClassName(StringMessage)

  {

   super(message);

   }

 }

 

Lets see an example that has the implementation of User Define Exception:

 

import java.io.*;
import java.util.*;

class MyException extends Exception
{
   private String nm="";
  public String getMessage(String s)
   {

   nm=s;
   return ("you are not permitted to enter inside "+nm);
   }
}

public class ExcepDemo
  {
  public static void main(String args[])throws MyException,IOException
  {

  String temp="";

  try
   {
   String str="amit";
   System.out.println("Enter the your name");
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  temp=br.readLine();

   if(!temp.equals(str))
  throw new MyException();
  else
   System.out.println("Welcome to Rose India");
   }
   catch(MyException e)
   {
  System.err.println(e.getMessage(temp));
   }
  catch(Exception e){
  System.err.println(e);
  }
   }
  }

 

Output of the program:

C:\Roseindia\>javac ExcepDemo.java

C:\Roseindia\>java ExcepDemo

Enter the your name

nisha

you are not permitted to enter inside nisha

C:\Roseindia\>java ExcepDemo

Enter the your name

amit

Welcome to Rose India

In this example we have created own exception class as MyException that throws an exception and a function with argument as getMessage that shows an exception message, if the user tries to enter the another name which doesn't match with a particular predefined name. After throwing  exception the control will be transferred  in the catch block to handle the exception where the function is  invoked to display the message included in that function.

Download this example

0