Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Exception Handling and Math Errors

Reply
Thread Tools

Exception Handling and Math Errors

 
 
Joe Hesse
Guest
Posts: n/a
 
      04-13-2004
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;
}

------------------------------------------------


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-13-2004
* "Joe Hesse" <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
Math.min and Math.max for byte / short Philipp Java 9 07-23-2008 12:37 AM
math.h trig functions questions (and some forgotten high school math) Mark Healey C Programming 7 05-22-2006 10:42 AM
Math::Infinity and Math::NaN Martin DeMello Ruby 4 04-29-2005 12:03 PM



Advertisments