Friday, 17 August 2012

How to create User Defined Exception

                                              


package com.example.exceptionjava;

class UserException extends Exception
{
   // Define a String variable
String error;

   // Constructor
public UserException(String msg)
  {
error = msg;
}

public String toString()
{
// return value
return ("error found :-"+error);
}
}

// main class
public class Myexception
 {
public static void main(String args[])
{
try
{
       //throw is used to create a new exception and throw it
throw new UserException("Your array out of range");
}
catch(UserException e)
{
      System.out.println(" "+e);
}
}

 }