• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Doubt with try catch

 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where is the proper place to return from a try/catch blok?
this method returns a boolean, (or a List, for example): so where should i return false (or null)?
inside the try i return true (or a List); should i return false inside the catch, after it, or even inside a finally block?

thanks in advance
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler will warn you if there is any execution path which will exit without returning a value.

What you return depends on the logic of your program and how it should proceed after an exception.

Bill
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

William Brogden wrote:The compiler will warn you if there is any execution path which will exit without returning a value.

What you return depends on the logic of your program and how it should proceed after an exception.

Bill


thanks for your answer, William
as to your first sentence, i'm well aware of it since i used compiler messages to set the return statement in place
but what i was asking is: even if the compiler agrees with my return, which is the proper place to return in case of failure? inside the catch block? after it? create a finally block to return the apropriated return in case of failure?
let me give an example:
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO it doesn't make sense to sometimes use retval, and sometimes not, and declare it before you return without it.

There's no one "right" way to do this.
 
Carlton Hanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to keep return statement's outside of my try, catch, finally blocks. I've ran into some bad debugging states otherwise.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see keeping them out of "finally" blocks, but I've never run in to a problem with them living in try/catch blocks. YMMV.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carlton Hanna wrote:


Slightly off-topic, I suggest you start closing your resources:
Note the nested try blocks; because PreparedStatement.close() can throw an SQLException of its own this allows you to have only one catch block. You should also nest try-finally blocks when working with ResultSets:
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good idea, thank you!
anyhow i would like your opinion on whether the following code makes sense along what you just said:

thanks in advance
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic