[Builder] Exception handling

I wrote small application showing the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <vcl.h> 
#pragma hdrstop 

#include "Unit1.h" 
#include "Unit2.h" 
#include "math.h" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm1 *Form1; 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
        : TForm(Owner) 
{ 

} 
//--------------------------------------------------------------------------- 


void __fastcall TForm1::RadioButton1Click(TObject *Sender) 
{ 
radio=17.0; 
} 
//--------------------------------------------------------------------------- 

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
int x; 
double y; 
x=Edit1->Text.ToInt(); 
bool checked; 
checked=CheckBox1->Checked; 

if(checked==true) 
{ 

        try 
        { 
                y=x*log10(radio); 
                Edit2->Text=AnsiString(y); 
        } 
        catch(...) 
        { 
                Form2->Show(); 
        } 
} 
} 
//--------------------------------------------------------------------------- 

Form1 consist of two Edit, Button, RadioButton and Checkbox.

The problem is that I don't know how to make the Form2 to appear insted of fallowing message:

log10: SING error

in situation when user forgetten to click Radiobutton.

Do You have any idea?
Thanks in advance.
I don't believe log10 throws exceptions.

Here's more information:
http://www.cplusplus.com/reference/clibrary/cmath/log10/
Last edited on
I 've read it.

When I don't click on Radiobutton variable radio contain default value (maybe 0), and I get SING error. That's how I think.

I'm beginner and I can be wrong.
Maybe
log10: SING error
is not thrown exception and that is way I can't catch it.
hmm...
Well, I'm in no way familiar with this function but I did check out google for a few ideas. It appears that log10 has a callback mechanism for exceptions on some systems. There is a function called matherr that gets called and I think you can provide one in your source. I am guessing that the default matherr creates the "SING" error. If that turns out to be true, then you could try providing an implementation of matherr that throws an exception instead.

Hope this helps.

EDIT: Actually, I don't see why there should be an error at all--unless you are providing an invalid value to the function. Start by printing out the value of radio.
Last edited on
I have read about matherr in http://poli.cs.vsb.cz/c/help/math0.htm#LBL54.

I am beginner in Builder and even in C++ and actually don't know how to implement matherr.

But matherr is connected with "math errors". What if exception is thrown by non-math function? How to handle exception in case radio is a argument of non-mathematical function.

There could be another function to which radio is passing but don't use it for mathematical calculation. In this situation error message will also occure.

In code appended above there is catch(...) which should catch any thrown exception. Those caused by mathematical and non-mathematical function.

Generally I want to preserve programm from not clicked Radiobutton producing popup window with proper message.
Topic archived. No new replies allowed.