?
Solved

how to port __try in UNIX

Posted on 2008-11-20
6
Medium Priority
?
1,077 Views
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.
__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

0
Comment
Question by:sandeepkulkarnib
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 23003168
Exceptions are a C++ feature, and are not part of C.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 23003177
The __try and __except are part of an MS extension :

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

Not for Unix ;)
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 23003186
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 23003205
>> 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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 23003263
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

862 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