• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt with try catch  RSS feed

 
miguel lisboa
Ranch Hand
Posts: 1281
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
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: 1281
  • 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:
 
Carlton Hanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
David Newton
Author
Rancher
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
Rancher
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: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java 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: 1281
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!