Click to See Complete Forum and Search --> : YATC (Yet Another Try Catch) (problem)


mikledet
January 28th, 2003, 06:03 AM
Hi all,

I have this code:

//CDsp.h file:

class CDsp {
public:
class Error: public runtime_error
{
public:

Error ();
Error (const string& msg);

};
//rest of the class CDsp
}
/////////////////////////////////
//CDsp.cpp file:

CDsp::Error::Error (const string& msg)
:runtime_error (msg)
{ }


CDsp::Error::Error ()
:runtime_error ("ptu: error")
{}
///////////////////////////////////
//in main:
int main()
{
//code

try {
foo(); //throws exception type CDsp:Error
}
catch(CDsp::Error &err)
{
cout<<err.what()<<endl;
// cout<<"bla"<<endl; //catch(...)
}

//code
}
/////////////////////////////////////////

The thing is, that the program never enters the
catch close, all though foo() is throwing an exception - could any one point to what am I doing wrong?
It behaves the same with catch(...)

Thanks in advance

Dani.

[Gabriel: edit: added [code ] tags]

Gabriel Fleseriu
January 28th, 2003, 07:28 AM
Hummm. I don't see any mistake. I'd say that either:
-foo() doesn't throw an exception
-you enter the catch(), but don't notice it because of some reason
-your compiler has a (major) bug. What compiler do you use?

TheCPUWizard
January 28th, 2003, 08:03 AM
Either that or the OP is doing something like:



throw new CDsp::Error("This will NOT be Caught");

dude_1967
January 28th, 2003, 08:15 AM
Dani,

The syntax of the catch is in proper order. I extracted your code-snippet and worked it into a complete example with slight mocifications (see code below).

I suspect, as Gabriel said, that foo does not properly throw an exception, or that the type of exception thrown is incorrect and does not properly harmonize with your catch. In the modified code, foo throws a std::bad_cast and the catch catches everything. Look at the example. Perhaps it will help you track down the problem with your code.

Chris.



#include <string>
#include <iostream>
using namespace std;

class runtime_error
{
private:

const string str;

public:

runtime_error(const string& msg) : str(msg)
{
}
virtual ~runtime_error()
{
}
};


static const void foo()
{
cout << "foo" << endl;
std::bad_cast bc;
throw(bc);
}

class CDsp {
public:
class Error: public ::runtime_error
{
public:

Error ();
Error (const string& msg);

};
//rest of the class CDsp
};

/////////////////////////////////
//CDsp.cpp file:

CDsp::Error::Error (const string& msg)
:runtime_error (msg)
{ }


CDsp::Error::Error ()
:runtime_error ("ptu: error")
{}
///////////////////////////////////
//in main:
int main()
{
//code

try {
foo(); //throws exception type CDsp:Error
}
catch(.../*CDsp::Error &err*/)
{
cout << "catch" << endl;
// cout<<err.what()<<endl;
// cout<<"bla"<<endl; //catch(...)
}

//code

return 1;
}

PaulWendt
January 28th, 2003, 08:33 AM
I think he's using runtime_error::what() and runtime_error::what
isn't returning his msg data member. His display in the catch
isn't printing out anything other than what() ... which I think to
be empty. Maybe if he overrides what() in his derived class?

--Paul

Edit: Nevermind; he's setting runtime_error's string value just
fine; I'm wacked out of my skull.

mikledet
January 28th, 2003, 08:34 AM
Thanks for your replys.
I am tottaly sorry for not giving the context of foo() so here it is
void CDsp::foo() throw(Error)
{

cout<<"foo"<<endl;
throw(" foo err ");
}

In addition, should the probelm with the type be a problem when using catch(..)?
In debug mode, the throw starts, and the app aborts somewhere in libc while dealing with the string, never getting back to main and the catch().
Is my foo() throw falty?

I am using KDevelop 2.0 on Linux 2.4.18...

CPUWIzardd - I just saw your post now -
Could explain a bit more...
Thanks.

Thanks again
Dani.
PUWIzardd - I just saw your post now -
Could explain a bit more...
Thanks.

PaulWendt
January 28th, 2003, 08:40 AM
Originally posted by mikledet
Thanks for your replys.
I am tottaly sorry for not giving the context of foo() so here it is
void CDsp::foo() throw(Error)
{

cout<<"foo"<<endl;
throw(" foo err ");
}


[/I]

You're saying that only CDsp::Error can be thrown in the
exception specification, but you're throwing a character string.
Because of this, unexpected_exception is called, which calls
terminate() or abort() or something like that. Don't throw a
character string; throw a CDsp::Error like you say you're going to.
Alternatively, you could take off the exception specification.

--Paul

mikledet
January 28th, 2003, 08:43 AM
Paul,(in respect to your first post)

I have thought of that as well, but 2 things:
1. If this was the problem, a catch(...) with a cout<<"bla"<<endl; should of worked - and it doesn't.
2. what() is virtual (not pure) in calss exception which runtime_error is derived from.
The origenal what() does what I need - why should I overload it?

Can you explain - why the what() returns nothing?


Thanks

Dani. [

mikledet
January 28th, 2003, 08:53 AM
Thanks Guys,

Thanks for the "nodge" on the head.

throw(Error("foo err"));
did the trick.

Thanks

Dani.

PaulWendt
January 28th, 2003, 09:54 AM
Originally posted by mikledet
Paul,(in respect to your first post)

I have thought of that as well, but 2 things:
1. If this was the problem, a catch(...) with a cout<<"bla"<<endl; should of worked - and it doesn't.
2. what() is virtual (not pure) in calss exception which runtime_error is derived from.
The origenal what() does what I need - why should I overload it?

Can you explain - why the what() returns nothing?


Thanks

Dani. [

Yeah, I made the post and did an edit later. For the record,
though, the cout << "bla" was commented out. <shrug> It's no
matter, though.

I came into the type of problem I discussed on another compiler
where there is no constructor handy.

--Paul