Exceptions

Discussion in 'C++' started by Carlos Martinez Garcia, Dec 21, 2005.

  1. 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
     
    Carlos Martinez Garcia, Dec 21, 2005
    #1
    1. Advertising

  2. Carlos Martinez Garcia

    Jim Langston Guest

    "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
     
    Jim Langston, Dec 21, 2005
    #2
    1. Advertising

  3. 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);
    };
     
    Neelesh Bodas, Dec 21, 2005
    #3
  4. 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(); }
     
    Maxim Yegorushkin, Dec 21, 2005
    #4
  5. Carlos Martinez Garcia

    Guest

    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
     
    , Dec 21, 2005
    #5
  6. wrote:

    []

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


    Sorry, my mistake.
     
    Maxim Yegorushkin, Dec 21, 2005
    #6
  7. Carlos Martinez Garcia

    mlimber Guest

    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
     
    mlimber, Dec 21, 2005
    #7
  8. 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.
     
    Christopher Benson-Manica, Dec 21, 2005
    #8
  9. Carlos Martinez Garcia

    mlimber Guest

    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
     
    mlimber, Dec 21, 2005
    #9
  10. Carlos Martinez Garcia

    Marcus Kwok Guest

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

    --
    Marcus Kwok
     
    Marcus Kwok, Dec 21, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Herr Lucifer
    Replies:
    0
    Views:
    564
    Herr Lucifer
    Jun 21, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,076
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,033
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    622
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    650
Loading...

Share This Page