how to port __try in UNIX

Posted on 2008-11-20
Last Modified: 2012-05-05
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

Question by:sandeepkulkarnib
    LVL 53

    Expert Comment

    Exceptions are a C++ feature, and are not part of C.
    LVL 53

    Accepted Solution

    The __try and __except are part of an MS extension :


    Not for Unix ;)
    LVL 68

    Expert Comment

    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

    LVL 53

    Expert Comment

    >> 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.
    LVL 68

    Expert Comment

    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 */

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
    I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now