• 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

Doubt in Exception Handling.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,

Please see the following 2 programs. could you please explain me why program 1 is giving compilation errors(complaining that it is expecting catch block) while program 2 is working fine without the catch block.

Only difference is , method in program 1 is void return type, whereas method in program 2 is integer return type. Is it permitted to avoid having a catch block in the method if the return type of the method is NOT void? I am confused.

Program 1:



Program 2:




Much appreciated,

thanks in advance,

Suresh.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the first program is going to throw a checked exception to the calling method. So you need to either catch it or use a throws clause with the method. The second code will also throw a checked exception, but the return statement in the finally block will suppress that exception. So you don't need a catch block. I would recommend that you try these two modified versions of the first program



 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new test().method(); is it a valid statement?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

piya rai wrote:new test().method(); is it a valid statement?



well, what do you think it should be?
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

piya rai wrote:new test().method(); is it a valid statement?


It's perfectly valid statement.
you can think it in this way also.
 
I will suppress my every urge. But not this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic