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

how to port __try in UNIX

I have a C program (as mentioned) to be ported to UNIX. Whenever I compile this code in UNIX, i get the following error:
`__try' undeclared (first use in this function)

The code section inside __try will surely throw error in some conditions and hence has to be caught.

Please suggest me the equivalent of __try/__except in the line of try/catch for C in UNIX.

Please make it quick as it is very essential for the project.
   /* Operation Code here */
   /* This will certainly throw error, which has to be caught in __except(1) */
   /* Some Code here */

Open in new window

  • 3
  • 2
1 Solution
Exceptions are a C++ feature, and are not part of C.
The __try and __except are part of an MS extension :


Not for Unix ;)
the __try / __catch mechanism works with
 "structured exceptions" and they exist only in windows.
Use the 'signal' subroutine instead and write your own signal handler.
I've attached a small description including a sample I got from

Signal Handling -- signal() 
An application program can specify a function called a signal handler to be invoked when a specific signal is received. When a signal handler is invoked on receipt of a signal, it is said to catch the signal. A process can deal with a signal in one of the following ways: 
The process can let the default action happen 
The process can block the signal (some signals cannot be ignored) 
the process can catch the signal with a handler. 
Signal handlers usually execute on the current stack of the process. This lets the signal handler return to the point that execution was interrupted in the process. This can be changed on a per-signal basis so that a signal handler executes on a special stack. If a process must resume in a different context than the interrupted one, it must restore the previous context itself 
Receiving signals is straighforward with the function: 
int (*signal(int sig, void (*func)()))() -- that is to say the function signal() will call the func functions if the process receives a signal sig. Signal returns a pointer to function func if successful or it returns an error to errno and -1 otherwise. 
func() can have three values: 
-- a pointer to a system default function SID_DFL(), which will terminate the process upon receipt of sig. 
-- a pointer to system ignore function SIG_IGN() which will disregard the sig action (UNLESS it is SIGKILL). 
A function address 
-- a user specified function. 
SIG_DFL and SIG_IGN are defined in signal.h (standard library) header file. 
Thus to ignore a ctrl-c command from the command line. we could do: 
   signal(SIGINT, SIG_IGN); 
TO reset system so that SIGINT causes a termination at any place in our program, we would do: 
   signal(SIGINT, SIG_DFL); 
So lets write a program to trap a ctrl-c but not quit on this signal. We have a function sigproc() that is executed when we trap a ctrl-c. We will also set another function to quit the program if it traps the SIGQUIT signal so we can terminate our program: 
#include <stdio.h>
void sigproc(void);
void quitproc(void); 
{ signal(SIGINT, sigproc);
		 signal(SIGQUIT, quitproc);
		 printf(``ctrl-c disabled use ctrl- to quitn'');
		 for(;;); /* infinite loop */}
void sigproc()
{ 		 signal(SIGINT, sigproc); /*  */
		 /* NOTE some versions of UNIX will reset signal to default
		 after each call. So for portability reset signal each time */
		 printf(``you have pressed ctrl-c n'');
void quitproc()
{ 		 printf(``ctrl- pressed to quitn'');
		 exit(0); /* normal exit status */

Open in new window

>> Use the 'signal' subroutine instead and write your own signal handler.

I don't think that's the best approach here ... I'd rather port the code to use proper error return codes rather than exceptions.

Or use a C++ compiler and use the C++ exceptions.
something like that? (Tested on AIX)
#include <sys/errno.h>
if [error_condition]
  printf("[some meaningful text]\n");
  printf("errno is %d:\n", errno);
  exit(errno);  /* or whichever way you want to handle the condition */
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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