Exception Handling and Math Errors



 DEVELOP > c-Plus-Plus > Exception Handling and Math Errors

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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?
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER