This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try / Catch Statements

 
Amy Caine
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is there any way, after an exception is handled, to return control back to the try block? I know Java uses the terminination model of exception handling and the try block is terminated, but is this possible?

Thanks,
Amy
 
Phil Hersh
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amy,

I'm not sure but I will answer your question indirectly. It is generally not a good idea to use try catch throw blocks to handle expected program events. It is for 'exceptional events'. What are you trying to do?

Phil
 
Amy Caine
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil,

Thanks for the reply. I have posted the code here (hopefully I do that whole code thing correctly).

If someone entered an invalid value, take "June", is there a way to display 'invalid month' instead of just the error line?

Thanks in advance!!

 
Amy Caine
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of errors in there; there is a missing '=' sign and a missing parens; sorry I was typing fast!! Thanks so much!
 
Amy Caine
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, last reply, I promise! I want to be able to print "Invalid Month" if someone enters "June" or a month name in the input box. I just noticed some more missing stuff... Sorry, I will take better care to retype next time...

Thanks so much!!
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the user enters something which is not an integer, NumberFormatException will be thrown by parseInt(...) and the control goes to your catch block of NumberFormatException. You can print invalid month in your catch block.
 
Amy Caine
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I just wanted to be sure. I can't find anywhere in my little book that I am reading that I would be able to return control back to the place that threw the exception.
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot return control directly back into the try block. If you had the whole try block wrapped in a loop that continues until it gets a valid month, then you can indirectly return to the try block by looping. It's often a good idea to shrink your try blocks around just the code that needs them.

Here's a simple example.Having an empty catch block simply says, "Yes, I see the error and don't care." It's called swallowing an exception.
 
Amy Caine
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David! I just tried this and it worked! I never knew you could do this! Woo Hoo! Now, which way is recommended? Which way is the best design?
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amy Caine:
Now, which way is recommended? Which way is the best design?
Glad to help! For this question, however, the answer is always "it depends on what you need." Do you need to loop and keep asking the user for a new month until they supply you with a valid one? If so, you'll want to wrap the JOptionPane call and parsing inside a while loop.Many people don't like the use of break, but I think it makes perfect sense for cases like this. Otherwise you'd have to repeat the validity test as the loop condition, but you already know month is invalid starting out, and you can use break instead to drop out once it's valid.

As I stated in my previous post, I generally shrink my try blocks down to the smallest size possible. The main reason is that then it's very clear which code can throw the exception. If you have a giant 50-line try block with three difference catch blocks, you'll have to read the full code to figure out which lines can cause errors. By splitting it up you communicate more information to the next person to read the code, possibly yourself in six months.

However, there are also many times where I have a block of code that may throw several types of exceptions, and I either want the whole thing to work or abort. In this case, I'll leave one big try block since I'm not going to recover or loop when an exception is thrown. I tend to do this in initialization code where either it all works or the program aborts.

When you're dealing with users, it's nice to give them an opportunity to recover from typos. But if you're reading configuration values from a file, all you can do when an error occurs is bail out and let the controller log an error message.
[ March 26, 2005: Message edited by: David Harkness ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, this is a pretty interesting thread. However, it is a little too advanced for the JiG (Beginner) forum, so I think I'll move it over to the JiG (Intermediate) forum.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a recursive approach without rangecheck:

[ March 27, 2005: Message edited by: Stefan Wagner ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic