Exception type

  • Thread starter Thread starter Maxim
  • Start date Start date
M

Maxim

Hi, All!

How do I get unmanaged exception type in the catch block. Here's the piece
of code to clarify my question.

try
{
throw int(1);
}
catch(Exception^ ex)
{
.............
}

How do I get the type (int) and the value (1) of the exception thrown?

Thanks in advance
 
Maxim said:
try
{
throw int(1);
}
catch(Exception^ ex)
{
............
}

How do I get the type (int) and the value (1) of the exception thrown?

I don't think System::Exception contains enough type information about
unmanaged exceptions. You have to catch int as an unmanaged exception.

You should always catch unmanaged exceptions first, then managed ones,
like this:

try
{
throw int(10);
//throw std::exception("a"); // try this too
}
catch(std::exception& ex)
{
Console::WriteLine("std::exception: " + gcnew String(ex.what()));
}
catch(int value)
{
Console::WriteLine("int: " + value.ToString());
}
catch(Exception^ ex)
{
Console::WriteLine("Exception^: " + ex->Message);
}

It doesn't matter if you catch int or std::exception first, but
Exception^ goes last. Also, the most derived type in the hierarchy goes
first:

try
{
}
catch(std::runtime_error& )
{
}
catch(std::exception& )
{
}

Tom
 
Thanks, Tom

Tamas Demjen said:
I don't think System::Exception contains enough type information about
unmanaged exceptions. You have to catch int as an unmanaged exception.

You should always catch unmanaged exceptions first, then managed ones,
like this:

try
{
throw int(10);
//throw std::exception("a"); // try this too
}
catch(std::exception& ex)
{
Console::WriteLine("std::exception: " + gcnew String(ex.what()));
}
catch(int value)
{
Console::WriteLine("int: " + value.ToString());
}
catch(Exception^ ex)
{
Console::WriteLine("Exception^: " + ex->Message);
}

It doesn't matter if you catch int or std::exception first, but Exception^
goes last. Also, the most derived type in the hierarchy goes first:

try
{
}
catch(std::runtime_error& )
{
}
catch(std::exception& )
{
}

Tom
 
Back
Top