Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

_matherr doesn't catch the exception

Status
Not open for further replies.

slapierre

Programmer
Feb 3, 2004
16
0
0
CA
Greetings,

I need to handle the exceptions generated by a call of the y = log10(x) when x equates 0 or is a negative number, I then count them and display the number of errors after my linear to logarithmic conversion. I wrote the following piece of code and it worked in another application, but it won't work now (the Log10ErrorCount is a global variable) :

int _matherr (struct _exception *Log10Error)
{
if (Log10Error->type == DOMAIN || Log10Error->type == SING)
{
Log10ErrorCount += 1;
return EXIT_FAILURE;
}
else
return EXIT_SUCCESS;
}

The only reason I think it isn't catching the exception is because I would have accidentally changed project options, which I didn't

Simon Lapierre
slapierre@sonomax.com

 
Howdy,

I am a bit confused by your above post, so I will ask for some additional information. I take it you are evaluating the function y = log10(x) for multiple values. Do you want to count all the exceptions that are risen by calculating individual "x"'s in a set of "x"'s?

One possibility would be this:
Code:
try
{
 //Whatever you do to calculate y=log10(x)
}
catch (const EZeroDivide &e)
{
 Log10ErrorCount += 1;
}

I think this may be what you need, BUT I am in the process of recovering from a Registry flameout, and I cannot test the above code at this time...

Good Luck,
onrdbandit

No! Try not. Do, or do not. There is no try. - Yoda
 
Hey Bandit!

Your reply is quite interresting but it only catches divisions by zero, the problem is that a y = log10(x) call will return a domain error when x = 0 and a sign error when x is a negative number. The data (an array of 512 values) is acquired by microphones wired to a DSP (which computes the FFT) and returned by a Win32 DLL.

I need to transform the linear response to a dB response. Some of the data has no physical meaning (voltage swings at the beginning and the end of the acquisition sequence) and will generate exceptions, here's the piece of the code you were asking for, after the transformation, the data is sent to a memo for display and sent to a TChart graph :

for (int i = 0; i < 512; ++i)
{
PH3_Modulus_dB = 20 * log10 (PH3[j]);
MEMO_SaveH3Data->Lines->Add (FloatToStrF (PH3_Modulus_dB, ffFixed, 15, LOW_PRECISION));
Series2->AddXY(SAMPLE_FREQUENCY*i, PH3_Modulus_dB, "", clBlue);
}

BTW, would you know how to handle login and empty fields
exceptions with databases (ODBC client driven by the BDE)

Simon Lapierre
slapierre@sonomax.com
 
<sound FX>SHHHHEEEEE FX>

Howdy,

(Oh by the way, ^that^ sound was your project zipping over my head)

Last question first... No. I have never done any work with ODBC, sorry. Try starting a new post maybe, but if you have not searched the help files, try that (people get frustrated when they have to post info found there).

Now, to the juice...
Code:
catch(...)
will catch any C++ exception

But, is there not a reason you can't test PH3[j] for being less than or equal to 0 before calculating? You know an error will occur then, BUT enclosing the calculation in a try-catch block would definately be a safe combination... (you never know what sort of weird errors can popup at times).

If you want to be more specific, search the help files for EMathError exception object. It has 5 descendants that describe math exceptions (one of which is EZeroDivide).

One finaly note: I just wanted to make sure you realized that in your posted code block, PH3[j] uses "j" as an index while the rest of the loop uses "i". This is a common mistake, and is easily overlooked. (Just wanted to make sure that is not a problem).

Good luck,
onrdbandit

No! Try not. Do, or do not. There is no try. - Yoda
 
Could you post more code? Like where are you using the try/catch block? Who's throwing the exception? etc...

Chris
 
Bandit! I sent you bits of pieces of codes, you deserve an explanation:

The 'j' increment isn't causing this problem, I only forgot to change it for an 'i', 'j' is used to decimate the array 'cause the PH3 buffer size is 1024, but I only take the even numbers (PH3 - polar form of the transfert function -> even numbers : modulus, odd numbers : phase)

here's the complete code :

float PH3_Modulus_dB[1024];
int j = 0;
for (int i = 0; i < 512; ++i)
{
PH3_Modulus_dB = 20 * log10 (PH3[j]);
j += 2;
}

How Interresting.. about that scanning the array before transformation, I could replace negative number and zeroes by x = 1, which will plot real zeroes instead of NAN and -INF sinxe log(1) = 0

Simon Lapierre
slapierre@sonomax.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top