• 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

Return statement in Catch block

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I started working on JDBC recently. As I was trying to understand the same, searched for a few programs on internet. I noticed in one program that the programmer has written a "return" statement in catch clause. I could not understand the significance of return in catch block.
Could you please help me understand this.

Example to the block:
catch (ClassNotFoundException e) {
System.out.println("Failed to load the driver");
e.printStackTrace();
return;
}
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

neha singh tomar wrote:Could you please help me understand this.


I think it might be better if you posted all the code for the method, because from what you've shown, there's no particular reason to put the return statement inside the block (although there's also nothing particularly wrong with it).

Winston
 
neha singh tomar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, Winston.

This has been copied from http://www.techlabs4u.com/2010/07/java-sample-code-to-connect-to-db2.html.
Please find the code below.

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello neha singh tomar,

Welcome to CodeRanch!

Pleae UseCodeTags so that code will be easier to read.

As you can see, in catch block, the code prints the stack trace. But if you don't put return statement there, code will proceed in the same method.

Now, in the given code, even if IBM DB2 driver is not found, without the return statement, code will follow saying 'driver loaded successfully' and attempting to get the connection, which we don't want.

The return statement simply returns from the method, without executing any further code.

Another approach for the same would be throwing your own exception in such cases.

I hope this helps.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

neha singh tomar wrote:Please find the code below.


First: please UseCodeTags (←click). (Edit: too late )

However, I see that the example is actually part of a main() method, so in it's case, the return statement will cause the program to exit. It seems a bit redundant to me, since you could achieve the same thing by simply declaring the method to throw ClassNotFoundException and forgetting the try...catch altogether; but maybe the authors were trying to provide a more user-friendly message.

In lots of these cases, there's no real 'right' or 'wrong' way to do things; just ones that make sense.

Winston
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But don’t use == true of == false, etc. Write if (b)... or if (!b)...
The == in that case is awkward to read, error-prone because you might write = by mistake.
reply
    Bookmark Topic Watch Topic
  • New Topic