UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » copy ctor on exception handling

#1 2005-07-28 10:33 PM

caruccio
Member
From: Porto Alegre, Brasil
Registered: 2005-02-08
Posts: 47

Re: copy ctor on exception handling

Hi all,

there is any way to handle a base class exception by simply catching it's derived class type ?

This is what I have:

class Exception : public std::exception
{
public:
   Exception() throw() {};
   Exception(const std::exception &e) throw() {};
};


int main()
{
 try {
    throw std::exception();
  } catch (Exception &e) {
    sdt::cout << "Exception captured" << std::endl;
    return 1;
  } catch (...) {
    std::cout << "Other exception captured" << std::endl;
    return 1;
  }

 return 0;
}

Since I've a Exception copy ctor for std::exception, shouldn't std::exception be catched ?

Offline

#2 2006-03-15 09:22 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: copy ctor on exception handling

Yay! I found an unanswered question...! 

It has been a while since the question was asked but what I figure what the heck... it looks lonely with that 0 in the reply column... ;-)

First off... you are working in the wrong direction... that would be the same as having two classes A and B where A was the parent class... declaring a variable of type A in your program... and then having the code invisibly boost your variable to type B... it wouldn't work...

The funny thing is this is still kind of a valid question... take this code... which is yours with two little modifications:

class Exception : public std::exception
{
public:
   Exception() throw() {};
   Exception(const std::exception &e) throw() {};
};


int main()
{
 std::exception ex = Exception( );

 try {
    throw ex;
  } catch (Exception &e) {
    std::cout << "Exception captured" << std::endl;
    return 1;
  } catch (...) {
    std::cout << "Other exception captured" << std::endl;
    return 1;
  }

 return 0;
}

Here the variable ex holds an Exception object and throws it.  You would probably think that the catch( Exception ) statement would catch it right?  After all it is a valid Exception object... but in fact what happens is that it gets caught by the (...) segment... The reason is that the system will treat it as a static copy of the same type as the variable was assigned to or the type it was cast to ( as long as that type is valid - lol ) in the throw statement... In this case Exception is assigned to ex which is declared as std::exception so that is what the copy is treated as...

In other words to get that caught as an Exception then either the variable ex would have to be of type Exception or you would have to use "throw ( Exception ) ex;"

If you want to make certain you are seeing the correct exception type that was thrown instead of potentially getting one of it's parent classes... the normal suggestion, including from the C++ FAQ is to do something along the lines of creating an exception base class... and have it and every child class declare the function:

virtual void raise( )
{
  throw * this;
}

Then instead of saying throw some_exception you would write some_exception.raise( ) which... because you are in the actual top level class of the exception... the static copy gets that same type and everything works as expected...

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2006-03-15 09:34 PM

caruccio
Member
From: Porto Alegre, Brasil
Registered: 2005-02-08
Posts: 47

Re: copy ctor on exception handling

a long time ago, in a far, far away post... :)

well, I see the picture, but my problem is to catch std::exception raised by libc++, without need to write catch code to both Exception and std::exception. I can't simply re-write exception class from lib to suppoer .raise(), as you suggested.

In other words, try/catch mechanism doesn't look for each catched type before enter each block. Any way to simulate this ?

Any tip ?

tnx,
Mateus

Offline

#4 2006-03-15 10:08 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: copy ctor on exception handling

Hmmm... now I am confused... or maybe I didn't understand the original question and I confused you...

I mean, in your example code the std::exception is caught by the catch(...) section... and if you didn't declare the catch Exception part, those also would be caught by catch(...)...

Lets start over...

1) something you are doing in your code is causing a C++ library to generate a std::exception ?
2) you are also generating your own Exceptions in the code for other reasons?
3) you want to catch both exceptions how?

Btw - what are you doing that is causing a std::exception to be thrown? 

Its late here ( I am in the Philippines right now and it is 5 am ) so I won't be back online for a while to answer any replies... I need some sleep ;-)

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#5 2006-03-15 10:31 PM

caruccio
Member
From: Porto Alegre, Brasil
Registered: 2005-02-08
Posts: 47

Re: copy ctor on exception handling

> 1) something you are doing in your code is causing a C++ library to generate a std::exception ?

yes, but i'm trying to handle an eventual exception.

> 2) you are also generating your own Exceptions in the code for other reasons?

yes, correct.

> 3) you want to catch both exceptions how?

I want to catch std::exception AND Exception ont the same catch block, as I  theorically did on the first post.

More specifically, when std::exception is raised, I want an Exception to be created from that raised std::exception and it shoud be handled on catch(Exception).

> Btw - what are you doing that is causing a std::exception to be thrown?

nothing, it's not beeing throwning (yet). I'm building a lib and should expect any stuppid thing fro my users.

> Its late here ( I am in the Philippines right now and it is 5 am ) so I won't be back online for a while to answer any replies... I need some sleep ;-)

some times sleep is essential.

I hope I made my self clear. If not, just forget...
thanks again.

Offline

#6 2006-03-16 05:53 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: copy ctor on exception handling

Well the only way to do that would be to wrap every library call... catch any potential exception that the call can throw... and re-wrap in you exception...

Personally I wouldn't worry about it...

My though is that if the library is throwing std::exception then that should normally be indicating a BAD error... and probably something that cannot be recovered from in a reasonable manner... so shouldn't be caught be a program... or should only be caught right at the base of you program to print out a trace and do an orderly shutdown...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

  • Index
  • » C++
  • » copy ctor on exception handling

Board footer

Powered by FluxBB