YATC (Yet Another Try Catch) (problem)
Hi all,
I have this code:
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]
and they were throwing down drinks like water...
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.
Code:
#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;
}