| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Joe Hesse" |
| Date: |
13 Apr 2004 12:20:21 PM |
| Object: |
Exception Handling and Math Errors |
What do I have to do to the following program to catch the runtime error?
Many thanks,
Joe Hesse
------------------------------------------------
#include <iostream>
#include <stdexcept>
using namespace std;
void f()
{
int x = 1, y = 0, z;
z = x/y; // divide by zero
}
int main()
{
try
{
f(); // function has a runtime error
}
catch(const runtime_error &re)
{
cerr << re.what() << "\n";
}
catch(...)
{
cerr << "Caught ..." << "\n";
}
return 0;
}
------------------------------------------------
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Exception Handling and Math Errors |
13 Apr 2004 01:01:34 PM |
|
|
* "Joe Hesse" <joe_hesse@actcx.com> schriebt:
What do I have to do to the following program to catch the runtime error?
AFAIK there's no fully platform-independent way to do that. One "nearly"
platform-independent way is to use a C SIGFPE signal (see 'signal') to set
a flag somewhere, which you can then check after the arithmetic. But in
practice this also involves platform-dependent code, so you might as well
go all the way in that direction -- just try to wrap it nicely up.
Note: you can use std::numeric_limits<double> to check what kind of support
your C++ implementation has for various floating point functionality.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|

|
Related Articles |
|
|