public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* try-catch block
@ 2002-08-27  0:51 Serguei I. Ivantsov
  2002-08-27  1:15 ` Andrew Snare
  0 siblings, 1 reply; 4+ messages in thread
From: Serguei I. Ivantsov @ 2002-08-27  0:51 UTC (permalink / raw)
  To: gcc

Hi!

Please help me to port this code to Linux from M$ (c++)
//...
try{
lock_semaphore();
something_terrible_function();  // can produce any kind of exception
unlock_semaphore();
}catch(...){
unlock_semaphore();
};
// ...


It is important that process must continue execution and unlock semaphore
after any exception in something_terrible_function()

--
 Regards,
  Serguei I. Ivantsov
   GSC Game World

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: try-catch block
  2002-08-27  0:51 try-catch block Serguei I. Ivantsov
@ 2002-08-27  1:15 ` Andrew Snare
  2002-08-27  9:21   ` Zack Weinberg
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Snare @ 2002-08-27  1:15 UTC (permalink / raw)
  To: Serguei I. Ivantsov, gcc

At 10:56 AM 27/08/2002 +0300, Serguei I. Ivantsov wrote:

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: try-catch block
  2002-08-27  1:15 ` Andrew Snare
@ 2002-08-27  9:21   ` Zack Weinberg
  2002-08-27  9:26     ` Andrew Haley
  0 siblings, 1 reply; 4+ messages in thread
From: Zack Weinberg @ 2002-08-27  9:21 UTC (permalink / raw)
  To: Andrew Snare; +Cc: Serguei I. Ivantsov, gcc

On Tue, Aug 27, 2002 at 10:16:18AM +0200, Andrew Snare wrote:
> At 10:56 AM 27/08/2002 +0300, Serguei I. Ivantsov wrote:
> >Hi!
> >
> >Please help me to port this code to Linux from M$ (c++)
> 
> This sort of question is a general C++ question, and probably not best sent 
> to this list.
> 
> >//...
> >try{
> >lock_semaphore();
> >something_terrible_function();  // can produce any kind of exception
> >unlock_semaphore();
> >}catch(...){
> >unlock_semaphore();
> >};
> >// ...
> >It is important that process must continue execution and unlock semaphore
> >after any exception in something_terrible_function()
> 
> The code-snippet that you pasted will work, although the exception that 
> occurs is not propagated. This sort of thing is best handled using the 
> 'initialization-is-acquistion' pattern described by Stroustrup (14.4 
> Resource Management).
...

This is the same guy who was asking about how to treat signals as
exceptions, yesterday.  That's a harder question, and at least
partially on topic for this list.

I believe that this fragment suffices to do that:

#include <csignal>

struct SignalAsException {
  int signo;
  SignalAsException(int s) : signo(s) {};
};

void handler(int signo) {
  throw new SignalAsException(signo);
}

void setup_signals(void) /* called from main */
{
  std::signal(SIGHUP, handler);
  std::signal(SIGINT, handler);
  std::signal(SIGILL, handler);
  std::signal(SIGBUS, handler);
  std::signal(SIGFPE, handler);
  std::signal(SIGSEGV, handler);
}

provided that the entire program, and all libraries it links, are
compiled with -fnon-call-exceptions.  (In production code I would use
sigaction and take care that the handler didn't leave signals blocked,
but that would unnecessarily complicate this example.)  Depending on
what the program does, the list of signals to handle may need to be
adjusted.

zw

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: try-catch block
  2002-08-27  9:21   ` Zack Weinberg
@ 2002-08-27  9:26     ` Andrew Haley
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Haley @ 2002-08-27  9:26 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: Andrew Snare, Serguei I. Ivantsov, gcc

Zack Weinberg writes:
 >  > 
 > I believe that this fragment suffices to do that:

[ ... ]

 > provided that the entire program, and all libraries it links, are
 > compiled with -fnon-call-exceptions.

and your compiler supports MD_FALLBACK_FRAME_STATE_FOR.

Andrew.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-08-27  9:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-27  0:51 try-catch block Serguei I. Ivantsov
2002-08-27  1:15 ` Andrew Snare
2002-08-27  9:21   ` Zack Weinberg
2002-08-27  9:26     ` Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).