public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Exception Handling
@ 2003-01-16 17:17 Ryan Cuprak
  2003-01-17  7:15 ` Michal Lipták
  0 siblings, 1 reply; 8+ messages in thread
From: Ryan Cuprak @ 2003-01-16 17:17 UTC (permalink / raw)
  To: gcc-help


Hello,
 Is it safe to throw an exception as such:
 throw new MyException ( "method","class","message);

 and then catch it as:
 try {
 }
 catch ( MyException *e ) {
   //-- do something
   delete e;
 }
 I am just looking for some guidelines on exceptions. In doing google searches
for info, I came across an email stating that the implementation of exceptions
varies by compiler in that some runtime systems do a binary copy of the
exception and then pass it to the catch etc.
 
Thanks,
 Ryan

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

* RE: Exception Handling
  2003-01-16 17:17 Exception Handling Ryan Cuprak
@ 2003-01-17  7:15 ` Michal Lipták
  2003-01-17 17:18   ` Oliver Kullmann
  0 siblings, 1 reply; 8+ messages in thread
From: Michal Lipták @ 2003-01-17  7:15 UTC (permalink / raw)
  To: gcc-help

I think it should be done like this:
throw MyException("method","class","message");

and then catch it:
try {
}
catch(MyException& e) {
}

because the new operator can throw as well.. can't it?
m.

> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
> Behalf Of Ryan Cuprak
> Sent: Thursday, January 16, 2003 6:15 PM
> To: gcc-help@gcc.gnu.org
> Subject: Exception Handling
> 
> 
> 
> Hello,
>  Is it safe to throw an exception as such:
>  throw new MyException ( "method","class","message);
> 
>  and then catch it as:
>  try {
>  }
>  catch ( MyException *e ) {
>    //-- do something
>    delete e;
>  }
>  I am just looking for some guidelines on exceptions. In doing 
> google searches
> for info, I came across an email stating that the implementation 
> of exceptions
> varies by compiler in that some runtime systems do a binary copy of the
> exception and then pass it to the catch etc.
>  
> Thanks,
>  Ryan
> 

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

* Re: Exception Handling
  2003-01-17  7:15 ` Michal Lipták
@ 2003-01-17 17:18   ` Oliver Kullmann
  0 siblings, 0 replies; 8+ messages in thread
From: Oliver Kullmann @ 2003-01-17 17:18 UTC (permalink / raw)
  To: gcc-help

> I think it should be done like this:
> throw MyException("method","class","message");
> 
> and then catch it:
> try {
> }
> catch(MyException& e) {
> }
> 
> because the new operator can throw as well.. can't it?
> m.
>

In "Gotcha #65: Improper Exception Mechanics" from

@Book{Dew2002,
  author =	 {Stephen C. Dewhurst},
  title = 	 {C++ Gotchas: Avoiding Common Problems in Coding and Design},
  publisher = 	 {Addison-Wesley},
  year = 	 2002,
  series =	 {Professional Computing Series},
  address =	 {Boston},
  note =	 {ISBN 0-321-12518-5; QA76.73.C153 D488 2002},
  annote =	 {Vorhanden.}
}

you can read more on why *anonymous temporary objects* should be thrown,
and caught by const- or non-const *reference* (in the above example
likely e is not altered, and thus "catch(const MyException& e)"
would be better).

To cite from [Dewhurst 2002]:


"When a throw-expression is executed, the runtime exception-handling mechanism
copies the exception object to a temporary in a "safe" location. ...
This means that the temporary will be usable until the last catch clause that
uses the temporary has completed, ... . This is an important property because,
to put it bluntly, when you throw an exception, all hell breaks loose. That
temporary is the calm in the eye of the exception-handling maelstrom.

This is why it's not a good idea to throw a pointer. ...

The address of the [object created by new] on the heap is copied to a safe
location, but the heap memory to which it refers is unprotected."


And you catch exception by reference (const or non-const) to avoid
slicing (and other problems). See Gotcha #65.

Also relevant here is Gotcha #64. And in the free electronic book

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

you find also valuable information on exception and how to handle them (volume 2).
(But w.r.t. exception specification the author (Eckel) recommends its use, while in a
recent CUJ article it was recommended NOT to use exception specification.)

Oliver
 
> > -----Original Message-----
> > From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
> > Behalf Of Ryan Cuprak
> > Sent: Thursday, January 16, 2003 6:15 PM
> > To: gcc-help@gcc.gnu.org
> > Subject: Exception Handling
> > 
> > 
> > 
> > Hello,
> >  Is it safe to throw an exception as such:
> >  throw new MyException ( "method","class","message);
> > 
> >  and then catch it as:
> >  try {
> >  }
> >  catch ( MyException *e ) {
> >    //-- do something
> >    delete e;
> >  }
> >  I am just looking for some guidelines on exceptions. In doing 
> > google searches
> > for info, I came across an email stating that the implementation 
> > of exceptions
> > varies by compiler in that some runtime systems do a binary copy of the
> > exception and then pass it to the catch etc.
> >  
> > Thanks,
> >  Ryan
> > 

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

* Re: Exception handling
  2017-05-29  9:00 Exception handling Flis, Przemyslaw (Nokia - PL/Wroclaw)
  2017-05-29  9:09 ` Florian Weimer
@ 2017-05-31  8:32 ` Andrew Haley
  1 sibling, 0 replies; 8+ messages in thread
From: Andrew Haley @ 2017-05-31  8:32 UTC (permalink / raw)
  To: gcc-help

On 29/05/17 10:00, Flis, Przemyslaw (Nokia - PL/Wroclaw) wrote:

> as far as I know, the way of handling exceptions "under the hood" is
> not defined in C++ standard. In "Technical report on C++
> performance" from 2008, I've found two main approaches to exception
> handling - so called "code" and "table" approach. Is there any way
> to determine which is used by gcc compiler?

I've never seen anything except "table".

> Does it depend on platform (i.e. ARM, x86 etc.) or compiler version?

It depends on the processor-speific ABI, which is defined by the
operating system.  For example,

http://refspecs.linuxfoundation.org/elf/x86_64-abi-0.95.pdf

at

http://refspecs.linuxfoundation.org/

-- 
Andrew Haley
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671

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

* Re: Exception handling
  2017-05-29  9:00 Exception handling Flis, Przemyslaw (Nokia - PL/Wroclaw)
@ 2017-05-29  9:09 ` Florian Weimer
  2017-05-31  8:32 ` Andrew Haley
  1 sibling, 0 replies; 8+ messages in thread
From: Florian Weimer @ 2017-05-29  9:09 UTC (permalink / raw)
  To: Flis, Przemyslaw (Nokia - PL/Wroclaw); +Cc: gcc-help

"Flis, Przemyslaw (Nokia - PL/Wroclaw)" <przemyslaw.flis@nokia.com>
writes:

> as far as I know, the way of handling exceptions "under the hood" is
> not defined in C++ standard. In "Technical report on C++ performance "
> from 2008, I've found two main approaches to exception handling - so
> called "code" and "table" approach. Is there any way to determine
> which is used by gcc compiler? Does it depend on platform (i.e. ARM,
> x86 etc.) or compiler version?

It depends on architecture, compiler version, and how GCC is built for
those architectures which support both approaches.  I think the the
default for the vast majority of architectures is table-based.

> As far I understand "table" approach has almost zero time overhead
> when exception does not appear. But when it does, is this time
> overhead predictable? If it is, how to predict it?

The overhead is not really predictable, except in very special cases.
It can be huge in multi-threaded programs:

  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71744

Florian

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

* Exception handling
@ 2017-05-29  9:00 Flis, Przemyslaw (Nokia - PL/Wroclaw)
  2017-05-29  9:09 ` Florian Weimer
  2017-05-31  8:32 ` Andrew Haley
  0 siblings, 2 replies; 8+ messages in thread
From: Flis, Przemyslaw (Nokia - PL/Wroclaw) @ 2017-05-29  9:00 UTC (permalink / raw)
  To: gcc-help

Hi,
as far as I know, the way of handling exceptions "under the hood" is not defined in C++ standard. In "Technical report on C++ performance " from 2008, I've found two main approaches to exception handling - so called "code" and "table" approach. Is there any way to determine which is used by gcc compiler? Does it depend on platform (i.e. ARM, x86 etc.) or compiler version? 
As far I understand "table" approach has almost zero time overhead when exception does not appear. But when it does, is this time overhead predictable? If it is, how to predict it?

Is there any newer report on C++ performance than one mentioned above? 

Best Regards
Przemyslaw Flis

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

* Re: Exception Handling
  2003-01-16 17:16 Exception Handling Ryan Cuprak
@ 2003-01-16 17:33 ` Nathan Sidwell
  0 siblings, 0 replies; 8+ messages in thread
From: Nathan Sidwell @ 2003-01-16 17:33 UTC (permalink / raw)
  To: rcuprak; +Cc: gcc-help

Ryan Cuprak wrote:
> Hello,
>  Is it safe to throw an exception as such:
>  throw new MyException ( "method","class","message);
> 
>  and then catch it as:
>  try {
>  }
>  catch ( MyException *e ) {
>    //-- do something
>    delete e;
>  }
this is safe

>  I am just looking for some guidelines on exceptions. In doing google searches
> for info, I came across an email stating that the implementation of exceptions
> varies by compiler in that some runtime systems do a binary copy of the
> exception and then pass it to the catch etc.

You are confusing the thrown object (a pointer) with the object it points
to (the new'd MyException).

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
          The voices in my head said this was stupid too
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org


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

* Exception Handling
@ 2003-01-16 17:16 Ryan Cuprak
  2003-01-16 17:33 ` Nathan Sidwell
  0 siblings, 1 reply; 8+ messages in thread
From: Ryan Cuprak @ 2003-01-16 17:16 UTC (permalink / raw)
  To: gcc-help


Hello,
 Is it safe to throw an exception as such:
 throw new MyException ( "method","class","message);

 and then catch it as:
 try {
 }
 catch ( MyException *e ) {
   //-- do something
   delete e;
 }
 I am just looking for some guidelines on exceptions. In doing google searches
for info, I came across an email stating that the implementation of exceptions
varies by compiler in that some runtime systems do a binary copy of the
exception and then pass it to the catch etc.
 
Thanks,
 Ryan

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

end of thread, other threads:[~2017-05-31  8:32 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-16 17:17 Exception Handling Ryan Cuprak
2003-01-17  7:15 ` Michal Lipták
2003-01-17 17:18   ` Oliver Kullmann
  -- strict thread matches above, loose matches on Subject: below --
2017-05-29  9:00 Exception handling Flis, Przemyslaw (Nokia - PL/Wroclaw)
2017-05-29  9:09 ` Florian Weimer
2017-05-31  8:32 ` Andrew Haley
2003-01-16 17:16 Exception Handling Ryan Cuprak
2003-01-16 17:33 ` Nathan Sidwell

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).