Re: Problem with dynamic_cast & exceptions using GCC 4?
Re: Problem with dynamic_cast & exceptions using GCC 4?
- Subject: Re: Problem with dynamic_cast & exceptions using GCC 4?
- From: Olivier Tristan <email@hidden>
- Date: Mon, 25 Jul 2005 19:17:51 +0200
Jesper Papmehl wrote:
Hi!
I am have trouble getting dynamic_cast to do the right thing when
compiling with GCC 4.
We use a hierarchy of exceptions (mainly the standard ones in
stdexcept) and in various places in out code, we use dynamic_cast to
convert between exceptions and error codes. We do this by catch:ing
std::exception references, and then try to dynamic_cast the caught
exception to the various exception classes to see which one it is.
This works fine in CodeWarrior and Visual Studio, and also in Xcode
when using GCC 3.3, but not when using GCC 4.
For example, when running the following program:
#include <iostream>
#include <stdexcept>
int main(int argc, char* const argv[]) {
try {
throw std::invalid_argument("");
}
catch (const std::exception& e) {
if (dynamic_cast<const std::invalid_argument*>(&e) != NULL) {
std::cout << "got invalid_argument" << std::endl;
}
else if (dynamic_cast<const std::logic_error*>(&e) != NULL) {
std::cout << "got logic_error" << std::endl;
}
else {
std::cout << "got something else" << std::endl;
}
}
return 0;
}
IMHO should work but you can try this to avoid the pb
#include <iostream>
#include <stdexcept>
int main(int argc, char* const argv[]) {
try {
throw std::invalid_argument("");
}
catch (const std::invalid_argument& e) {
std::cout << "got invalid_argument" << std::endl;
}
catch (const std::logic_error& e) {
std::cout << "got logic_error" << std::endl;
}
catch(..)
{
std::cout << "got something else" << std::endl;
}
return 0;
}
Hope this helps
--
Olivier Tristan
Ultimate Sound Bank
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden