Exceptions

  • Thread starter Carlos Martinez Garcia
  • Start date
C

Carlos Martinez Garcia

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
 
N

Neelesh Bodas

Jim said:
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);
};
 
M

Maxim Yegorushkin

Carlos said:
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(); }
 
D

deane_gavin

Maxim said:
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
 
M

Maxim Yegorushkin

(e-mail address removed) wrote:

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

Sorry, my mistake.
 
M

mlimber

Carlos said:
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
 
M

mlimber

Christopher said:
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
 
M

Marcus Kwok

Christopher Benson-Manica said:
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/default.asp?url=/library/en-us/dndeepc/html/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++).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top