• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exceptions - handle or declare rule

 
Ranch Hand
Posts: 47
C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 47
C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would advise you to read this. And please UseCodeTags.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic