• 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

thrown exception in catch clause

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
do we need method to declare that it throws exception if it throws uncaught exception in catch block

eg

class A{
void mymethod () // or shd it be void mymethod() throws Exception
{
throw new Exception();}

catch(Exception e){

throw new Exception();}

}}
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
off course u need to declare or rather the method should declare that it throws an exception. the program will terminate otherwise
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mohit,

The answer is yes if you throw throw another exception within a catch block instead of simply treat the previous one.

In your example, your can either put the second throw statement within another try-catch block or add a throws clause to method's signature.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Don't you think throws comes into consideration only if you are throwing checked exceptions. Do you think Exception is a checked exception. Because I remember that Exception class is not a checked exception. Its not necessary that you have to have a throws exception. You can write in this way also.

Assuming that Exception is a checked Exception if it is not.





throws tells to the calling functions to take care of the checked exception as I cannot do that.

Hope I have answered your question.

Golden Rule: When an exception occurs it should be either handled or thrown

 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the assumption that I made that Exception is invalid for throws declaration.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic