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

how to catch the Arithmetic exception?

I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}

After ran the program, it quitted with core dumped.
%test
Arithmetic exception (core dumped)

who can tell me an appropriate approach?
Jul 19 '05 #1
10 30221
Gary.Hu wrote:
I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}

After ran the program, it quitted with core dumped.
%test
Arithmetic exception (core dumped)

who can tell me an appropriate approach?


When all else fails, why not try
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
It's probably an exception of type std::runtime_error.

--
Emacs is a nice OS, but I prefer UNIX.

Jul 19 '05 #2
"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:1168213.OcskaVrlgv@klesk...
Gary.Hu wrote:
I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}

After ran the program, it quitted with core dumped.
%test
Arithmetic exception (core dumped)

who can tell me an appropriate approach?


When all else fails, why not try
catch(std::exception& e){
std::cout << e.what() << std::endl;
}


Is there any reason why catch(...) wouldn't catch exceptions that are
catched by catch(std::exception& e)...?

"Arithmetic exception" is probably an OS or processor exception which is
not the same as a C++ exception, hence it can not be catched by a
try/catch block. If you ask me you need platform and/or compiler
specific constructs to handle "Arithmetic exceptions".

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #3
On Mon, 21 Jul 2003 14:45:28 -0700, "Gary.Hu" <hu****@metarnet.com>
wrote in comp.lang.c++:
I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}

After ran the program, it quitted with core dumped.
%test
Arithmetic exception (core dumped)

who can tell me an appropriate approach?


Arithmetic errors of this type are not C++ exceptions, they are
undefined behavior. Your compiler might provide a way to turn them
into C++ exceptions, but it is not required to.

There are no requirements or guarantees on what might happen when you
generate undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #4

"Gary.Hu" <hu****@metarnet.com> wrote in message news:bf***********@mail.cn99.com...
| I was trying to catch the Arithmetic exception, unsuccessfully.
| try{
| int a = 0, b = 9;
| b = b / a;
| }catch(...){
| cout << "arithmetic exception was catched!" << endl;
| }
|
| After ran the program, it quitted with core dumped.
| %test
| Arithmetic exception (core dumped)
|
| who can tell me an appropriate approach?

Others have already indicated why you cannot catch these kind
of errors with standard C++ exception handlers, but why not
make a simple check, and then throw one of the available
'standard' exceptions yourself ?.

For example:

int main()
{
try
{
int a = 0, b = 9;
if( a == 0 )
throw std::runtime_error( "Divide by zero encountered. " );

b /= a;
}
catch( const std::runtime_error& e )
{
cout << e.what() << endl;
}

return 0;
}

Personally, I think using exceptions here is a bit of an overkill
anyway. What's wrong with a simple conditional check ?, such as:

if( a == 0 )
cout << "Divide by zero encountered. " << endl;
else
b /= a;

If you can handle the error locally, then why not ?
Exceptions are often misused, and should be avoided
if you *really* don't need them.

Cheers.
Chris Val
Jul 19 '05 #5
Peter van Merkerk wrote:
When all else fails, why not try
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
Is there any reason why catch(...) wouldn't catch exceptions that are
catched by catch(std::exception& e)...?


catch(...) catches everything; I'm not trying to say otherwise. Allow me to
rephrase my sentence:
"When all other attempts at deducing the actual type of an exception have
failed, why not try std::exception which is the base of all exceptions
generated by the standard library".

"Arithmetic exception" is probably an OS or processor exception which is
not the same as a C++ exception, hence it can not be catched by a
try/catch block. If you ask me you need platform and/or compiler
specific constructs to handle "Arithmetic exceptions".


Ah well... "If [...] the result is not mathematically defined or not in the
range of representable values for its type, the behaviour is undefined"
which, as we all know, means that all bets are off.

--
It isn't that unix isn't a user friendly operating system,
it's just choosy about which users it wants to be friends
with, and even the best of friends occasionally fight.

Jul 19 '05 #6
"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:2360088.KJvXMtpkjA@klesk...
Peter van Merkerk wrote:
When all else fails, why not try
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
Is there any reason why catch(...) wouldn't catch exceptions that are
catched by catch(std::exception& e)...?


catch(...) catches everything;

catch(...) catches all exceptions, but it does not catch a 'Divide by zero'
error. (unless, of course, the compiler generates code which throws
automatically on divide by zero, but this does not seem to be the case here)
I'm not trying to say otherwise. Allow me to
rephrase my sentence:
"When all other attempts at deducing the actual type of an exception have
failed, why not try std::exception which is the base of all exceptions
generated by the standard library".
....because std::exception is the base _only_ of exceptions generated by the
standard library (derived from std::exception) . There are, however, many,
many more exceptions which are not derived from std::excteption, but still
can be thrown, for example

class MyError{};
throw MyError();

The most general form is still catch(...).

"Arithmetic exception" is probably an OS or processor exception which is
not the same as a C++ exception, hence it can not be catched by a
try/catch block. If you ask me you need platform and/or compiler
specific constructs to handle "Arithmetic exceptions".
Ah well... "If [...] the result is not mathematically defined or not in

the range of representable values for its type, the behaviour is undefined"
which, as we all know, means that all bets are off.

--
It isn't that unix isn't a user friendly operating system,
it's just choosy about which users it wants to be friends
with, and even the best of friends occasionally fight.

Jul 19 '05 #7

"Klaus Eichner" <kl******@yahoo.com> wrote in message
news:3f**********************@news.free.fr...
| "Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
| news:2360088.KJvXMtpkjA@klesk...
| > Peter van Merkerk wrote:

[snip]

| > I'm not trying to say otherwise. Allow me to
| > rephrase my sentence:
| > "When all other attempts at deducing the actual type of an exception have
| > failed, why not try std::exception which is the base of all exceptions
| > generated by the standard library".
|
| ...because std::exception is the base _only_ of exceptions generated by the
| standard library (derived from std::exception) . There are, however, many,
| many more exceptions which are not derived from std::excteption, but still
| can be thrown, for example
|
| class MyError{};
| throw MyError();
|
| The most general form is still catch(...).

Are you saying that 'catch( ... );' should be used ?

....rather than: catch( const MyError& e ); ?

AFAIU, the former should only be used when you don't
know what will be thrown.

Cheers.
Chris Val
Jul 19 '05 #8

"Chris ( Val )" <ch******@bigpond.com.au> wrote in message
news:bf************@ID-110726.news.uni-berlin.de...

"Klaus Eichner" <kl******@yahoo.com> wrote in message
news:3f**********************@news.free.fr...
| "Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
| news:2360088.KJvXMtpkjA@klesk...
| > Peter van Merkerk wrote:

[snip]

| > I'm not trying to say otherwise. Allow me to
| > rephrase my sentence:
| > "When all other attempts at deducing the actual type of an exception have | > failed, why not try std::exception which is the base of all exceptions
| > generated by the standard library".
|
| ...because std::exception is the base _only_ of exceptions generated by the | standard library (derived from std::exception) . There are, however, many, | many more exceptions which are not derived from std::excteption, but still | can be thrown, for example
|
| class MyError{};
| throw MyError();
|
| The most general form is still catch(...).

Are you saying that 'catch( ... );' should be used ?

...rather than: catch( const MyError& e ); ?
Yes, the reasoning behind that is as follows:

- it is not guaranteed that a compiler generates code such that an exception
is thrown if an arithmetic exception (such as "divide by zero") is
encountered during run-time.
AFAIU, the former should only be used when you don't
know what will be thrown.
- if the compiler generates such code, it is not known of what type the
exception is, in particular we don't know whether or not this exception is
derived from std::exception.
Cheers.
Chris Val

Jul 19 '05 #9
"Chris ( Val )" <ch******@bigpond.com.au> wrote in message
news:bf************@ID-110726.news.uni-berlin.de...

"Klaus Eichner" <kl******@yahoo.com> wrote in message

[snip]

| > | class MyError{};
| > | throw MyError();
| > |
| > | The most general form is still catch(...).
| >
| > Are you saying that 'catch( ... );' should be used ?
| >
| > ...rather than: catch( const MyError& e ); ?
|
| Yes, the reasoning behind that is as follows:
|
| - it is not guaranteed that a compiler generates code such that an exception | is thrown if an arithmetic exception (such as "divide by zero") is
| encountered during run-time.

That part I know, I wasn't questioning this aspect of it :-).

| > AFAIU, the former should only be used when you don't
| > know what will be thrown.
|
| - if the compiler generates such code, it is not known of what type the
| exception is, in particular we don't know whether or not this exception is | derived from std::exception.

I was asking, why you would use only 'catch( ... );', rather
than 'catch( const MyError& e );', because, if you use a
conditional check, then you get to choose the exception
thrown.

For example:

try
{
int a = 0, b = 9;
if( a == 0 )
throw MyError( "Divide by zero encountered. " );

b /= a;
}
catch( const MyError& e )
{
cout << e.what() << endl;
}
catch( ... )
{
cout << "Something unknown happened" << endl;
}

I am asking, why use only 'catch( ... );' for the above, when
1) it won't catch it,
if 'catch(...)' won't catch it, then nothing will catch it. i.e. there is no
solution to the problem, but we don't know, but at least we can try.
2) we know that MyError will get caught
in the handler specified,
This is not true in the original example. It only works in your example
where you added an '...if ( a == 0 ) throw (MyError("xxx"));...', but this
was not the original scenario. The original scenario was a program which
performed a simple division.

I think here is where the confusion starts:

I introduced 'MyError' in a previous post:
=== There are, however, many,
=== many more exceptions which are not derived from std::excteption, but
still
=== can be thrown, for example class MyError{};

What I wanted to say is that the *compiler* is allowed to throw an exception
of any type, even 'MyError' is allowed (where 'MyError' is a compiler
specific exception type)

Of course, I didn't express myself clearly, and you assumed that 'MyError'
was thrown as part of an '...if ( a == 0 ) throw (MyError("xxx"));...' in a
modified version of the C++ program.

I must apologise for the inaccurate post previously, and I hope that my post
is now clearer.
and 3), it should only be used for
*unknown* exceptions that *can* be handled as a C++ exception,
as a safety net being the last handler in the try/catch mechanism.
that's exactly the situation described in the original example: a C++
compiler can generate code such that the program execution throws an
exception if it encounters a "divide by zero", but it is not required by the
standard to do so. Furthermore, in case an exception is thrown, the standard
does not require a specific type of exception.

In other words: The exception (if it is thrown at all) in the original
example is *unknown*.

As you corectly said, 'catch(...)' should be used for those unknown
exceptions as a safety net.
Having it as the last handler above is ok, because I know for this
particular 'div by zero' error, 'MyError' will be caught. The only
reason for the last handler, is to catch the catchable unknown.

Cheers.
Chris Val



Jul 19 '05 #10

"Klaus Eichner" <kl******@yahoo.com> wrote in message
news:3f**********************@news.free.fr...
| "Chris ( Val )" <ch******@bigpond.com.au> wrote in message

[snip]

| What I wanted to say is that the *compiler* is allowed to throw an exception
| of any type, even 'MyError' is allowed (where 'MyError' is a compiler
| specific exception type)
|
| Of course, I didn't express myself clearly, and you assumed that 'MyError'
| was thrown as part of an '...if ( a == 0 ) throw (MyError("xxx"));...' in a
| modified version of the C++ program.
|
| I must apologise for the inaccurate post previously, and I hope that my post
| is now clearer.
|
| > and 3), it should only be used for
| > *unknown* exceptions that *can* be handled as a C++ exception,
| > as a safety net being the last handler in the try/catch mechanism.
|
| that's exactly the situation described in the original example: a C++
| compiler can generate code such that the program execution throws an
| exception if it encounters a "divide by zero", but it is not required by the
| standard to do so. Furthermore, in case an exception is thrown, the standard
| does not require a specific type of exception.
|
| In other words: The exception (if it is thrown at all) in the original
| example is *unknown*.
|
| As you corectly said, 'catch(...)' should be used for those unknown
| exceptions as a safety net.

You're right, I did misinterpreted what you were trying to explain <g>.

I agree with what you have stated.

Cheers.
Chris Val

PS: Sorry about the late reply, I completely forgot
about this thread :-).
Jul 19 '05 #11

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

Similar topics

16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
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...
3
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
11
by: l.woods | last post by:
I want to set up my CATCH for a specific exception, but I really don't know which one of the multitude that it is. I am getting the exception now with Catch ex as Exception but I want to be...
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...
3
by: linq936 | last post by:
I have some algorithm dealing with unsigned int calculation a lot, I am trying to add some check for overflow. The initial thinking was very easy, just do something like this, int...
3
by: George2 | last post by:
Hello everyone, I have tested try-catch works with structured exception, to my surprise. Previously I think we have to use __try and __except. Any comments? Here is my test code and I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.