GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 170 of 493    Print  
Please Give me an example for User defined Exception

  
Total Answers and Comments: 7 Last Update: February 15, 2008     Asked by: chandu 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
November 17, 2005 02:46:13   #1  
N B Patil        

RE: Please Give me an example for User defined Excepti...
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); } //}
 
Is this answer useful? Yes | No
November 17, 2005 05:17:42   #2  
simran        

RE: Please Give me an example for User defined Excepti...
User defined exceptions are exceptions uncured handled by user by defining extended class from exception and throws,throw keyword
 
Is this answer useful? Yes | No
November 19, 2005 09:19:56   #3  
alajangi rajeswari        

RE: Please Give me an example for User defined Excepti...
throw
 
Is this answer useful? Yes | No
November 20, 2005 00:35:03   #4  
shankar singh        

RE: Please Give me an example for User defined Excepti...
Please Give me an example for User defined Exception
Answer:

 
Is this answer useful? Yes | No
November 22, 2005 06:36:05   #5  
nagaraju.balla        

RE: Please Give me an example for User defined Excepti...

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");}

 }}

         



 


 
Is this answer useful? Yes | No
November 28, 2005 08:52:39   #6  
nagarajcse82 Member Since: November 2005   Contribution: 4    

RE: Please Give me an example for User defined Excepti...

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);

}

}


 
Is this answer useful? Yes | No
February 15, 2008 13:21:04   #7  
sampra Member Since: February 2008   Contribution: 279    

RE: Please Give me an example for User defined Exception.
Exception created by  our own  is called User define Exception
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape