Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

Error handling

Hi Experts,

   I am working with Visual C++.NET 2005 and I am looking for a way to do generic error handling.  A way to trap all errors even if I don't know what kind of error it is.

Something like this:

Try
{

}

Catch(...)
{
     // Here, I want to be able to somehow retreive the error message the OS generated
}

Thanks,
axnst2
0
axnst2
Asked:
axnst2
2 Solutions
 
AlexFMCommented:
Managed or unmanaged C++?
In both cases, catching all exceptions is strongly NOT recommended. This effectively hides program bugs. Programming guidelines require catching only expected exceptions. For example, format exception reading user input, or I/O exception working with files. Every unexpected exception must remain unhandled, this is only way to fix program bugs.
0
 
axnst2Author Commented:
I am working with unmanaged C++.

BTW, I know that the excepted way of handling errors is that you shouldn't catch all exceptions, that you should only catch exceptions, which are likely to happen; however, I think that's so stupid.  Exceptions that I can expect to happen, like I/O issues when I'm writing to a file, I check for 95% of the possible sources of issues before I even execute my write/read command.  Instead, I think it's much more reasonable to hve error catching for the problems I can't possibly (or very unlikely to) foresee.  All I want is to be able to tell what the error was and where it happened so that I can pinpoint and then fix the problem.


Thanks,
axnst2
0
 
AkumasCommented:
well, to my knowledge, it impossible to get exception details with catch(...).
to me, the only useage of catch(...) is to prevent release version from crash, but if you want to solve problem, you can't use catch(...) to ignore it.

maybe you can warp unknown exception in function instead of in function caller.

void functionA()
{
  try{}
  catch(...){throw ExceptionInA;}
}

int main()
{
  try{
   functionA();
  }
catch(ExceptionInA)
{
  //process exception
}
}
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now