• 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

throw new Exception() confusion

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the below example, i thought when we encounter the line 'throw new Exception();', it would actually go back to the calling method (main in this case). But why it is getting into catch block?
Then under what circutances would this throw new Exception() will goto the calling method?
Please help. Thanks in advance
 
Sekhar Kadiyala
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i missed telling you the output. It would be 13423
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sekhar,


what circutances would this throw new Exception() will goto the calling method?


If you remove the try,catch and finally blocks and declare the exception(throws Exception)in your method declaration:

it will be propogated to the calling method.
Since you are handling it it will not be propagated.
Sindhur.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right - when an exception is thrown, it will be caught by the nearest matching catch block. If the exception is thrown within a try block that has a matching catch block and/or finally block, the exception is handled there.
If the exception is thrown within a try block without a matching catch or finally block (or it's a RuntimeException), the exception will be propogated to the method that invoked this one. It will then be handled there or, if that method doesn't handle the exception, it will propogate to the method that invoked that one, and so on.
Of course, if you're not going to handle a given exception within a method that might throw one, you must declare that the method in question is capable of throwing such an exception (by adding "throws Exception" to the end of the method signature). Once you've done that, any methods that invoke this one will be required to handle that exception (or throw it).
 
Sekhar Kadiyala
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys! I learned one more thing!! Just 5 more days!!! i can't wait to complete the exam!!!
 
no wonder he is so sad, he hasn't seen 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