aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exceptions - handle or declare rule Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exceptions - handle or declare rule" Watch "exceptions - handle or declare rule" New topic
Author

exceptions - handle or declare rule

pankaj saxena
Ranch Hand

Joined: Nov 21, 2009
Posts: 46
sir,

could any body tell me, what is handle or declare rule in Exceptions by one example?

thanks in advance.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Handling means, you are handling the exception rather than let some one to handle. And declaring means, you are declaring the this method will throw exceptions, and the the user's responsibility to handle that exception!

Does this make sense?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
pankaj saxena
Ranch Hand

Joined: Nov 21, 2009
Posts: 46
sir,


could you please tell me the meaning of the following code:

1.
class TestExt{
void doStuff(){
throw new MyException();
}
}

My questions are:
a) what is the meaning of "throw new MyException()"?
b) Do we require MyException class in hand before writing the class TestExt ?


2.
class MyException extends Exception{
void doStuff() throws MyException{
try{
throw new MyException();
}catch(MyException me){}
}
}

MY question is:
a) what is the meaning of "void doStuff() throws MyException" ? why throws MyException is attached with method?
b) what is the meaning of " throw new MyException()" in try block?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I would advise you to read this. And please UseCodeTags.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1



a) what is the meaning of "throw new MyException()"?

  • throw is keyword in java used to throw either exceptions or errors
  • new is used to create a new object
  • MyException is our user defined exception as we are extending the Exception class

  • b) Do we require MyException class in hand before writing the class TestExt ?

    There is no need to write the MyException class before the TestEx class



    a) what is the meaning of "void doStuff() throws MyException" ? why throws MyException is attached with method?

  • throws is a keyword that says "the method or the init block can throw exception"
  • void doStuff throws MyException means "dostuff method CAN throw MyException i.e. doStuff code can cause MyException"

  • writing above means we are declaring the exception

    and writing the code in try catch block means handling the exception

    hope this helps

    SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
    If you find any post useful, click the "plus one" sign on the right
    Shanky Sohar
    Ranch Hand

    Joined: Mar 17, 2010
    Posts: 1051

    Hi Pankaj

    Could you please USECODETAGS while posting your query.
    It would be easier to read and also you will get a good response.


    SCJP6.0,My blog Ranchers from Delhi
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: exceptions - handle or declare rule