Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Exceptions

Reply
Thread Tools

Exceptions

 
 
Carlos Martinez Garcia
Guest
Posts: n/a
 
      12-21-2005
Hi all:

I have a class with a method that throws an exception.

class A {
public:
void myMethod() throw(MyException);
};

My question is if MyExcepcion must be defined at that point or not.
I think it must because if I use a forward declaration of MyExcepcion,
compiler gives me an error.

Is there any valid way without include the definition of MyException
before A?

Thanks in advance
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      12-21-2005

"Carlos Martinez Garcia" <> wrote in message
news:dob87q$...
> Hi all:
>
> I have a class with a method that throws an exception.
>


class MyException;

> class A {
> public:
> void myMethod() throw(MyException);
> };
>
> My question is if MyExcepcion must be defined at that point or not.
> I think it must because if I use a forward declaration of MyExcepcion,
> compiler gives me an error.
>
> Is there any valid way without include the definition of MyException
> before A?
>
> Thanks in advance



 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-21-2005

Jim Langston wrote:
> class MyException;


Isn't the definition also necessary?
The following code fails to compile (g++3.4.2, Comeau)

class MyException;
class A {
public:
void myMethod() throw(MyException);
};

 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      12-21-2005

Carlos Martinez Garcia wrote:
> Hi all:
>
> I have a class with a method that throws an exception.
>
> class A {
> public:
> void myMethod() throw(MyException);
> };
>
> My question is if MyExcepcion must be defined at that point or not.
> I think it must because if I use a forward declaration of MyExcepcion,
> compiler gives me an error.


The definition is necessary.

> Is there any valid way without include the definition of MyException
> before A?


Declare a function that throws the exception and use that function
isntead of throw statement.

extern void throw_my_exception();

// void myMethod() throw(MyException);
void myMethod() { throw_my_exception(); }

 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-21-2005

Maxim Yegorushkin wrote:
> Carlos Martinez Garcia wrote:
> Declare a function that throws the exception and use that function
> isntead of throw statement.
>
> extern void throw_my_exception();
>
> // void myMethod() throw(MyException);
> void myMethod() { throw_my_exception(); }


The OP's original code was using an exception specification, not
necessarily throwing an exception. Your code isn't evuivalent.

I nearly made the same mistake since I never see exception
specifications in the real world.

Gavin Deane

 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      12-21-2005

wrote:

[]

> The OP's original code was using an exception specification, not
> necessarily throwing an exception. Your code isn't evuivalent.


Sorry, my mistake.

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      12-21-2005
Carlos Martinez Garcia wrote:
> Hi all:
>
> I have a class with a method that throws an exception.
>
> class A {
> public:
> void myMethod() throw(MyException);
> };
>
> My question is if MyExcepcion must be defined at that point or not.
> I think it must because if I use a forward declaration of MyExcepcion,
> compiler gives me an error.
>
> Is there any valid way without include the definition of MyException
> before A?
>
> Thanks in advance


The experts suggest not using exception specifications at all. See
these links:

http://www.gotw.ca/publications/mill22.htm

http://www.gotw.ca/gotw/082.htm

A quote from the first:

"So here's what seems to be the best advice we as a community have
learned as of today:
"Moral #1: Never write an exception specification.
"Moral #2: Except possibly an empty one, but if I were you I'd
avoid even that."

Not using such specs would likely eliminate your problem.

Cheers! --M

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-21-2005
mlimber <> wrote:

> The experts suggest not using exception specifications at all. See
> these links:


> http://www.gotw.ca/publications/mill22.htm


That's a very informative article. Thanks for posting it. Does it
reflect the consensus of c.l.c++ posters, do you think?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      12-21-2005
Christopher Benson-Manica wrote:
> mlimber <> wrote:
>
> > The experts suggest not using exception specifications at all. See
> > these links:

>
> > http://www.gotw.ca/publications/mill22.htm

>
> That's a very informative article. Thanks for posting it. Does it
> reflect the consensus of c.l.c++ posters, do you think?


It does.

Cheers! --M

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      12-21-2005
Christopher Benson-Manica <> wrote:
> mlimber <> wrote:
>
>> The experts suggest not using exception specifications at all. See
>> these links:

>
>> http://www.gotw.ca/publications/mill22.htm

>
> That's a very informative article. Thanks for posting it. Does it
> reflect the consensus of c.l.c++ posters, do you think?


As far as I have seen, yes. It probably also doesn't help that a
popular compiler (MS's) doesn't even handle std::unexpected properly
(see
http://msdn.microsoft.com/library/de...deep111899.asp
(and the next article also, Part 12) for an explanation; it is an MSDN
site but most of the article refers to standard C++).

--
Marcus Kwok
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
ImageList Errors - Exceptions and...need HELP Herr Lucifer ASP .Net 0 06-21-2004 10:21 AM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments