How We Can We write our own exceptions?

Showing Answers 1 - 6 of 6 Answers

kuriakose

  • Oct 1st, 2005
 

create a user-defind class that extends Throwable class. An object of a class that extends Throwable can be thrown and caught. 

  Was this answer useful?  Yes

S.V.Sita Kiran

  • Oct 1st, 2005
 

We can write our own exception by deriving our class from Exception class and implementing the toString method with in own way.

  Was this answer useful?  Yes

d

  • Mar 8th, 2006
 

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

  Was this answer useful?  Yes

maddy_me

  • Sep 22nd, 2009
 

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

}

  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