• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

Trapping an error!

I use try & catch functions to trap errors in my program.
The problem is I can't trap error like divisionbyzero or any error causing a 'core dump' in LINUX. The only error I trap is the one I throw!

Example:
void myFunc()
{
  //int a = 9/0;
  throw("BOGUS!");
}

void main()
{
  try{
    myFunc();
  }
  catch(CExeption e){//Do something}
  catch(...){//Do something}
}

I can't trap system's errors like the division by zero! Is it possible to trap everything because I don't want the program to end... I want to deal with those unexpected errors!!

Thanks!
0
slabrie2
Asked:
slabrie2
1 Solution
 
jkrCommented:
>>I can't trap system's errors

These are usually handled by 'signal()' handlers. You would actually use the signal hander to throw the exception for you.

0
 
ExceterCommented:
Can you use assert?

For example,

int x = 0, y = 6;

assert( x > 0 && y > 0 );

x = y / x; // The program will never get here because the assertion will fail.

If the assertion fails it will spit out a diagnostic message and the line number in the code.

Exceter
0
 
darlingmCommented:
jkr is right, it's all handled by signal().

One of my programs catches CTRL+C.  I will first explain how that one works, then what do modify to do what you're looking for.

at top of main:

if(signal(SIGINF, ctrlCHandler) == SIG_ERR) {
   printf("Could not install signal handler!\n");
}

then define in your source:

void ctrlCHandler(int signal) {
   exit(0);
}

This handler doesn't appear to do anything at first glance, but it allows objects destructors to run so they can shut down nicely before the program actually stops running.

You need to change SIGINT to SIGFPE to trap floating point exceptions, which is raised even when there is an integer divide by zero.

I tried this before submitting my comment, to be able to trap integer divide by zero's, and I'm running into an issue that wasn't there in my CTRL+C case because in that case I exit the program.  Basically you run into ctrlCHandler() being called time after time again -- if you put a cout into there, it wraps the text off the top of your screen.

What's happening is that when my ctrlCHandler returns, the signal isn't being lowered -- so it's called again.  You need to in that function tell the system to lower the SIGFPE.  I've spent 20 minutes looking around on the web for how to do this, and everything I tried failed.

I wish you better luck google'ing than I had, or maybe someone else knows how to lower a signal.

jkr?
0
 
slabrie2Author Commented:
Yes!!!
Thanks darlingm, it's working!
There's only one bad thing... it's about the signal that I can't remove. It's kind of looping, getting back to ctrlCHandler().  I try to throw() an error in this function to be able to catch it, because I think if I do nothing in ctrlCHandler(), the system is trying the same line again... the division by zero.

But it's not working! I get a aborted (core dump)!

Having any idea?
0
 
slabrie2Author Commented:
But, do you know the way to handle this signal without ending the process?

Example for divide by zero.

Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now