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.
__try
{
   /* Operation Code here */
   /* This will certainly throw error, which has to be caught in __except(1) */
}
__except(1)
{
   /* Some Code here */
}

Open in new window

sandeepkulkarnibAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
Exceptions are a C++ feature, and are not part of C.
0
Infinity08Commented:
The __try and __except are part of an MS extension :

        http://msdn.microsoft.com/en-us/library/s58ftw19.aspx

Not for Unix ;)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
woolmilkporcCommented:
Hi,
 
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
 
http://www.cs.cf.ac.uk/Dave/C/node24.html#SECTION002420000000000000000
 
wmp



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: 
 
 
SIG_DFL 
-- a pointer to a system default function SID_DFL(), which will terminate the process upon receipt of sig. 
SIG_IGN 
-- 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); 
 
main()
{ 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

0
Infinity08Commented:
>> 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.
0
woolmilkporcCommented:
Hi,
 
something like that? (Tested on AIX)
#include <sys/errno.h>
if [error_condition]
 {
  printf("[some meaningful text]\n");
  printf("errno is %d:\n", errno);
  perror(NULL);
  exit(errno);  /* or whichever way you want to handle the condition */
  }
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.