Trapping an error!

Posted on 2003-02-26
Medium Priority
Last Modified: 2008-02-01
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!

void myFunc()
  //int a = 9/0;

void main()
  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!!

Question by:slabrie2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 8027514
>>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.


Expert Comment

ID: 8028176
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.


Accepted Solution

darlingm earned 200 total points
ID: 8029702
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) {

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.


Author Comment

ID: 8034106
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?

Author Comment

ID: 8044447
But, do you know the way to handle this signal without ending the process?

Example for divide by zero.


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question