473,403 Members | 2,366 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,403 software developers and data experts.

Good practice for returning "status" from functions

Hi there,

I am writing an application that performs calculations on records
within a data table. There may be many records in a data table.

There are situations where the calculation may not be able to return a
meaningful value (e.g. Divide by zero) and null (or other default) is
returned instead. This is not a fatal error, so processing can
continue on the rest of the data table. As such, the table may
ultimately end up with a mixture of some correctly calculated and
uncalculated values.

In that situation I would like to be able to warn the user that some
values could not be calculated. Ideally I would like to be able to
tell them the records where this occurred.

I have used C# for a number of years and understand how to use
Exceptions, and Inner Exceptions. However exceptions are not
appropriate here as processing continued.

How do other developers deal with returning information about status?
Do you return a single status code (e.g. -204, which can be translated
into a helpful string such as "division by zero occurred in some
records"), or something more complex? Alternatively, have Microsoft
implemented something in .NET that I have yet to find?

Thanks in anticipation

Roger

Jun 22 '07 #1
3 1636
I'm not sure I understand why exceptions are not appropriate for your case.
Your custom return values are just a different packaging mechanism to an
exception. Your receiver code handling the custom return value is just a
different mechanism for handling what normally would be an exception. I
would think your exception handler from the client would be coded similarly
to what your code does for the custom return value, but does so based on the
exception message or a customized exception property.

"ro**********@talk21.com" wrote:
Hi there,

I am writing an application that performs calculations on records
within a data table. There may be many records in a data table.

There are situations where the calculation may not be able to return a
meaningful value (e.g. Divide by zero) and null (or other default) is
returned instead. This is not a fatal error, so processing can
continue on the rest of the data table. As such, the table may
ultimately end up with a mixture of some correctly calculated and
uncalculated values.

In that situation I would like to be able to warn the user that some
values could not be calculated. Ideally I would like to be able to
tell them the records where this occurred.

I have used C# for a number of years and understand how to use
Exceptions, and Inner Exceptions. However exceptions are not
appropriate here as processing continued.

How do other developers deal with returning information about status?
Do you return a single status code (e.g. -204, which can be translated
into a helpful string such as "division by zero occurred in some
records"), or something more complex? Alternatively, have Microsoft
implemented something in .NET that I have yet to find?

Thanks in anticipation

Roger

Jun 22 '07 #2
A fair point

However, throwing an exception will terminate processing as something
"couldn't be done". In this case what the user wanted couldn't be
done, so the default value was used in stead, and then the processing
can continue with the next records.
Hope that helps.

Roger

Jun 22 '07 #3
There are a number of things you can do. First, I agree, Exceptions have a
different purpose and meaning, so you should implement a solution that works
for you.

given the idea signature divide(decimal,decimal) decimal

We know that there are reasons that you cannot perform the division, one is
that you cannot divide by zero. You expect an exception to occur in this
condition, and since you can account for it it will not break stability in
the program.

So you can do a couple things to get informative information.

A tried and true method is similar to the dialog result.

divide( decimal, decimal, decimal) mathresult

the first to inputs are the inputs for the calculation the third is the
outbound result of the computation and mathresult is either an enum of
status messages or an object containing informative information.

My other ideas are really variations upon this.
Say you have a more complex calculations to perform, for me it would be
rating an insurance premium.

function compute(policy as Policy) as PolicyRate
end funciton

Here I must pass in the entire policy, loop over each item with coverage,
then over each coverage or coverage group and compute a rate, then sum up
all the rates for each item with coverage, then sum up for the policy.
Should a calculation fail, or we can determine bad values, we pass back
informative help for the user so they may change their selections.

dim pr as PolicyRate = RatingEngine.Compute(policy)
if not pr.status = Success then
'tell the user of their bobo
dim dlg as new PolicyErrorDialog(pr)
dlg.show(me)
else
'Display the computed rates
dim dlg as new RatingWorksheet(pr)
dlg.show(me)
end if

Certainly, I have exception management in my calculation routines and even a
generic handler. I have decision trees wrapped around the known potential
issues and in the event of something unexpected, I make note of it and stop
the calculation. Flow of the application remains constant.

try
catch (calculationExceptions) 'Based on Logic
'Prevent minor calculations, most likely a bad value or unexpected result
catch (databaseExceptions) 'Based on environment
'Prevent lookups for factors and surcharges, Network group must be at it
again.
catch (Exception) 'Who knows
'stops calculation processing for an unexpected reason
end try

<ro**********@talk21.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
>A fair point

However, throwing an exception will terminate processing as something
"couldn't be done". In this case what the user wanted couldn't be
done, so the default value was used in stead, and then the processing
can continue with the next records.
Hope that helps.

Roger

Jun 22 '07 #4

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

Similar topics

4
by: Christian Eriksson | last post by:
Hi! I want to clarify, for myself, some basic facts about Oracle Client Server configuration. I start with the listener configuration on the server side. What block(s) in what configuration...
0
by: John Yasaitis | last post by:
I seem to have replication set up correctly to only replicate one database named vmail.* Except when I try to update a table in vmail.* the changes do not replicate to the slave. Authentication...
0
by: wxqun | last post by:
I'm try tuning a SELECT statement which is used by a Cognos cube on a DB2 V7.2 +FP13. After I did the tuning, the "dynexpln" shows the "total cost" is down to 8214567 from 37345265, also I try to...
3
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - I have window.status="Moomin"; why doesn't the statusbar change?...
1
by: Ronen Yacov | last post by:
Hi There, I've using IE 7.0.5730.11 on Windows XP. I want to open a pop up using javascript without showing the status bar, and to do so I use the function: function openwindow() {...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.