LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2004, 10:11 AM   #1
Sinner6
LQ Newbie
 
Registered: Mar 2004
Distribution: United Linux(they forced me to)
Posts: 16

Rep: Reputation: 0
try catch(...)


I am a windows programmer who is porting an app to linux. It is going well so far but I am hitting a seg-fault. My error handing code seems to be at issue.

Code

try {
ReallyUglyFunction(); //throw lots of std::exceptions plus many others including divide by zero or referencing bad memory
}
catch (exception e) {
// catches what I expect
}
catch (...) {
// never catches anything in linux
}


In Windows catch(...) is a fail safe that catches everything.
In Linux catch(...) doesn't seem to catch anything.

What do I need to do to fix this?

Thanks

Last edited by Sinner6; 06-22-2004 at 11:49 AM.
 
Old 06-22-2004, 11:26 PM   #2
mascdman
Member
 
Registered: Jun 2004
Location: Canada
Distribution: Ubuntu 6.06 LTS
Posts: 152

Rep: Reputation: 30
From what I've seen, errors like division by zero and segmentation violations result in signals being forced on a process by the kernel. You can install signal handlers in your C++ program like so:
Code:
#include <exception>
#include <iostream>
#include <signal.h>

using namespace std;

void sighandler(int);

void sighandler(int signum)
{
  switch(signum)
    {
    case 8:  cout << "Caught SIGFPE\n"; exit(1);
    case 11: cout << "Caught SIGSEGV\n"; exit(2);
    default: cout << "I shouldn't be here!\n";
    }
}

int main(void)
{
  int a = 1, b = 0;
  int* d = 0;

  if (signal(SIGFPE, &sighandler) == SIG_ERR)
      cout << "Couldn't install FPE signal handler!\nExpect a core dump\n";
  if (signal(SIGSEGV, &sighandler) == SIG_ERR)
      cout << "Couldn't install SEGV signal handler!\nExpect a core dump\n";

#ifndef SKIP
  cout << "Div by zero\n";
  int c = a/b;
#else
  cout << "Playing with bad memory\n";
  d[0] = 0;
#endif
}
Once you've run and compiled, you should get
Code:
[mascdman@odin tmp][ :) ]$ ./a.out 
Div by zero
Caught SIGFPE
[mascdman@odin tmp][ :( 1 ]$
or
Code:
[mascdman@odin tmp][ :) ]$ ./a.out 
Playing with bad memory
Caught SIGSEGV
[mascdman@odin tmp][ :( 2 ]$
To get a list of other signals, run man -S 7 signal. Hope this helps.

--mascdman
 
Old 06-24-2004, 07:22 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Re: try catch(...)

Quote:
Originally posted by Sinner6
try {
ReallyUglyFunction(); //throw lots of std::exceptions plus many others including divide by zero or referencing bad memory
}
catch (exception e) {
// catches what I expect
}
catch (...) {
// never catches anything in linux
}
Can you be more specific? Like in actually posting some
code, including the output you get on screen?


Cheers,
Tink
 
Old 06-25-2004, 10:22 AM   #4
Sinner6
LQ Newbie
 
Registered: Mar 2004
Distribution: United Linux(they forced me to)
Posts: 16

Original Poster
Rep: Reputation: 0
I think I have things back under control.
Thanks for all the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Gentoo and Catch 22? bigjohn Linux - General 5 11-12-2004 07:14 PM
C++ catch ctrl+c true_atlantis Programming 1 10-01-2004 12:23 PM
Catch signals C++ moyacuba Programming 2 03-18-2004 01:28 PM
Well...I'll catch you all later... Whitehat General 30 03-11-2004 10:32 PM
Try Catch for VB.net kazuni Programming 3 01-31-2004 01:06 AM


All times are GMT -5. The time now is 10:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration