Please Give me an example for User defined Exception

Showing Answers 1 - 9 of 9 Answers

N B Patil

  • Nov 17th, 2005
 

extend the Class from Exception and you can use the same for your purposewith either of the four constructors Exception() Constructs a new exception with null as its detail message.Exception(String message) Constructs a new exception with the specified detail message.Exception(String message, Throwable cause) Constructs a new exception with the specified detail message and cause.Exception(Throwable cause) Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).MyException extends Exception { MyExeception(String msg) { super(msg); } //}

  Was this answer useful?  Yes

simran

  • Nov 17th, 2005
 

User defined exceptions are exceptions uncured handled by user by defining extended class from exception and throws,throw keyword

  Was this answer useful?  Yes

alajangi rajeswari

  • Nov 19th, 2005
 

throw

  Was this answer useful?  Yes

shankar singh

  • Nov 20th, 2005
 

Please Give me an example for User defined Exception
Answer:

  Was this answer useful?  Yes

nagaraju.balla

  • Nov 22nd, 2005
 

class NoMoneyException extends Exception{

       public NoMoneyException(String m){super(m);}}

       public class Bank {

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

      int b=5000,wd=15000;

    if(b>wd){System.out.println("Enjoy");}

  else {throw new NoMoneyException("Insufficient Money Exception");}

 }}

         



 

  Was this answer useful?  Yes

in case of system defined exceptions,if any violation occured while executing the code,it will create a exception object and throw that object.

in catch block u catch it and give appropriate message.

userdefined exception:in case of user defined exceptions,create a class as a subclass of java.lang.Throwable and implement   toString() method in your class.

Example:

public class UserException extends java.lang.Throwable{

private String ex;

public UserException()

{

}

public UserException(String s)

{
    ex=s;

}

public String toString()

{

return ex;

}

}

public class App{

public static void main(String args[])

{

//take the value from key board   if it is equal to exception generate user defined exception

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

try{

if(br.readLine()=="exception"){


    UserExceptin ue=new User Exception("this is the user defined exception");

throw ue;

}

}

catch(Exception e)

{
System.out.println(e);

}

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions