Thread: Exception Handling Problem

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    Question Exception Handling Problem

    Hi guys!
    I'm new to programming in C++ on Linux so i run into a strange problem. I was trying to catch segmentation fault but i couldnt. Though i placed try..catch block around the code that tries to write some value to the begining of RAM my program successfully crashed and i only saw "SegmentationFault" as its output. So my question is how to catch segmentation faults in C++ on Linux. Any help appreciated.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can use "signal()" to catch a segment fault.

    http://www.hmug.org/man/3/signal.php

    Segmentation fault is number 11 (SIGSEGV).

    Note however that it's not guaranteed that you can continue from a SIGSEGV - but you should be able to catch the fault.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Note however that it's not guaranteed that you can continue from a SIGSEGV - but you should be able to catch the fault.
    Just don't try to be clever by throwing a C++ exception from inside the SIGSEGV handler. It would be awesome if that worked, but it doesn't.

    According to POSIX, the behavior of a program is undefined after it received an asynchronous SIGSEGV. In practice, many system will attempt to retry the faulting instruction after the handler finishes. If you return from the signal handler, chances are it's just going to be called again, in an infinite loop. The only thing you can really do is print an error (carefully!) and call exit().

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    According to POSIX, the behavior of a program is undefined after it received an asynchronous SIGSEGV. In practice, many system will attempt to retry the faulting instruction after the handler finishes. If you return from the signal handler, chances are it's just going to be called again, in an infinite loop. The only thing you can really do is print an error (carefully!) and call exit().
    Or, you could use setjmp()/longjmp() to continue executing somewhere completely different.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Or, you could use setjmp()/longjmp() to continue executing somewhere completely different.
    Thereby jumping past all kinds of destructors and leaking resources right and left?

    But that's a problem for C programs as well. At least in C you can build an "unwind list" which you can process prior to a longjmp(). In C++ it's nearly impossible because there is no way to deliberately trigger a destructor. Well, you can call ~Class() directly, but that doesn't release memory since it has no idea whether the object is heap or stack... It's a gnarly problem.

    In practice, just set a global flag from your signal handler, and periodically check it in the main body of code. If you see that it's set, THEN you can throw an exception and everything will unwind properly. But that doesn't work for SIGSEGV for reasons already mentioned...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exception handling wrapper functions?
    By m37h0d in forum C++ Programming
    Replies: 17
    Last Post: 06-10-2008, 02:56 PM
  2. exception handling, function call and memory
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 01-30-2008, 08:00 AM
  3. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  4. file handling problem
    By aditya1 in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2005, 05:29 AM
  5. Exception handling !!!
    By Brain Cell in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2005, 06:25 PM