PDA

View Full Version : use try cacth exception



jindoniit
30th August 2011, 05:46
Can i use "try catch" in app?
I try to write a simple example ,in app I use "try catch" but unsuccess?
Can i help you?

Lykurg
30th August 2011, 06:30
Show us what you tried so far.

jindoniit
30th August 2011, 07:11
i tried

int t =1;
int t1 = 0;
try
{
float t2 = t/t1;
}
catch (...)
{
qDebug()<<"test";
}

FelixB
30th August 2011, 07:51
and what happens?

jindoniit
30th August 2011, 08:26
app is wrong. why try catch do not active?

FelixB
30th August 2011, 08:33
what means "app is wrong"?

jindoniit
30th August 2011, 08:45
app is quit and it does not run command "Qdebug()<<"test"

gkarthick5
30th August 2011, 09:22
What compiler are you using? I think in some cases, the compiler doesn't generate an exception

helloworld
30th August 2011, 09:42
Division by zero doesn't throw an exception in Standard C++.

jindoniit
30th August 2011, 10:28
if Qt does not catch exceptions, it will crashes...
so, does Qt catch exceptions anywhere?

FelixB
30th August 2011, 10:32
what does Qt have to do with this? it's c++ that does not throw an exception in this case, as helloworld stated.

you can use try...catch in your qt app. you can catch exceptions. but you can't catch exceptions that are never thrown...

wysota
30th August 2011, 12:45
so, does Qt catch exceptions anywhere?
Yes, it does. Not that this matters in any way...

SixDegrees
30th August 2011, 17:22
Floating point errors don't throw exceptions. They generate signals. Not Qt signals.

If you need to respond to them, you need to install your own system signal handler. See the C++ signal() call for information on how to do this.

If all you want to do is avoid division by zero, simply check that the divisor is not equal to zero before performing the operation, and either throw your own exception or put your own error handling in place.

jindoniit
31st August 2011, 03:11
Thanks all for reply
I only want to do is avoid app crashes by catch unforeseen error

wysota
31st August 2011, 07:47
Thanks all for reply
I only want to do is avoid app crashes by catch unforeseen error

Then fix the errors. Exceptions won't do that for you. They are mostly for handling forseen errors.