GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 105 of 507    Print  
How We Can We write our own exceptions?

  
Total Answers and Comments: 4 Last Update: September 22, 2009     Asked by: chandu 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
October 01, 2005 01:55:54   
kuriakose        

RE: How We Can We write our own exceptions?
create a user-defind class that extends Throwable class. An object of a class that extends Throwable can be thrown and caught.
 
Is this answer useful? Yes | No
October 01, 2005 02:04:11   
S.V.Sita Kiran        

Core Java Sun Press
We can write our own exception by deriving our class from Exception class and implementing the toString method with in own way.
 
Is this answer useful? Yes | No
March 08, 2006 23:43:05   
d        

RE: How We Can We write our own exceptions?

To write our own Exception class

1)Our class must be subclass of Throwable or Exception class

2)Evert Exception class provide 2 constructors

a) no arguments

b) one argument i.e String argument

3) Optional: Overidding toString metod of java.lang.Object


 
Is this answer useful? Yes | No
September 22, 2009 10:27:34   
maddy_me Member Since: September 2009   Contribution: 3    

RE: How We Can We write our own exceptions?
we can write our own exceptions by extending them either from Throwable or from Exception.

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

try
{
int a 1/0;
} catch(Exception e)
{
throw new MyException(); //our own exception
}

}
}
and you can write your own exception class as:
public class MyException extends Exception {

public MyException()
{
System.out.println("Hello ! You got Arithmetic Exception.");
}

}

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape