• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exceptions - handle or declare rule

 
pankaj saxena
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir,

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

thanks in advance.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
pankaj saxena
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would advise you to read this. And please UseCodeTags.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Pankaj

    Could you please USECODETAGS while posting your query.
    It would be easier to read and also you will get a good response.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic