473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Newby - avoiding catch for code flow vs reuse of validation logic in framework

Hi all,

In the code I'm working on to learn Java, I wanted to check to see if a string
can be converted to a BigDecimal, and get the BigDecimal value if so. There is
no assumption that the string is probably a valid BigDecimal, just that it might
be, and I want to get the converted value if so.

In my code, I used a Try with an empty Catch so that the string conversion would
either succeed or fail which would both test and do the conversion in 1 step.
This seems to break 2 commonly quoted best practices, though. One is to never
use an empty Catch, and the other is to never use exception handling for
something other than exception handling. The alternatives, though, seem to
involve me writing my own code to check the validity of the string as a
BigDecimal which would be duplicating logic that is already in the standard
classes (the code that validates and raises the exception) and increases the
likelihood of bugs.

Opinions?

Thanks,

- Steve Jorgensen
Jul 17 '05 #1
2 1592
In my opinion, you are taking the correct approach. While rules
aren't always made to be broken, the two you cite certainly strike me
as candidates.

The admonition against using exceptions for non-exceptional behavior
is to avoid using the throw/catch mechanism for flow control. For the
most part, the "structured programming" principles that came into
vogue in the late 70s provide most, if not all, of the flow control
constructs one would need. So the idea is, don't use throw/catch
where if (condition) {...} will do.

As for never having empty catch blocks, I am unsure where this comes
from. Within the context of the BigDecimal class, encountering
non-decimal characters is exceptional. But within your code, you
simply don't care. So your code should express that.

Cheers!
- Jerry Oberle
Jul 17 '05 #2
On 25 Jul 2003 19:53:54 -0700, go*****@hotmail.com (Gerard Oberle) wrote:
In my opinion, you are taking the correct approach. While rules
aren't always made to be broken, the two you cite certainly strike me
as candidates.

The admonition against using exceptions for non-exceptional behavior
is to avoid using the throw/catch mechanism for flow control. For the
most part, the "structured programming" principles that came into
vogue in the late 70s provide most, if not all, of the flow control
constructs one would need. So the idea is, don't use throw/catch
where if (condition) {...} will do.

As for never having empty catch blocks, I am unsure where this comes
from. Within the context of the BigDecimal class, encountering
non-decimal characters is exceptional. But within your code, you
simply don't care. So your code should express that.

Cheers!
- Jerry Oberle


Thanks very much for your insight.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: David S. | last post by:
I am looking for a way to implement the same simple validation on many instance attributes and I thought descriptors (http://users.rcn.com/python/download/Descriptor.htm) looked like the right...
3
by: Aaron | last post by:
How would code obfuscation affect performance? in .net C#
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
11
by: Wolfgang Kaml | last post by:
I am not sure if this is more of an expert question, but I am sure that they are out there. I'd like to setup a general application or bin directory on my Win2003.Net Server that will hold some...
4
by: Fraggle | last post by:
Hi, I want to validate a text box, The user can leave it blank, or fill it in. If they fill it in then it must be a date within a certain range. How can I achieve this? Thank you very much
5
by: apandapion | last post by:
I'm working with csharp and .net for the first time, and I've had a fair amount of luck. I started with the MSDN "Walkthrough : Creating a Distributed Application" tutorial and expanded from...
9
by: Bob Achgill | last post by:
I really like this function but have tried to slow down on using it because I get a 1 second pause each time I use it. I don't really understand why the computer has to think for 1 second! ...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.