Archived

This topic is now archived and is closed to further replies.

Throw & Catch

This topic is 5579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, sorry, could''nt find this on google:
  
int k=0;

  try
  {
    k/=k;  
  } catch (...)
  {
    MessageBox(NULL, "Some Exception", "Exception", MB_OK);	
  };
  
On the code above, I catch a divide by zero exception. Now, as I''m told "catch(...)" will catch everything, how can i discriminate what I am catching? Something like this:
  
  try
  {
    do_stuff();
  } catch (...)
  {
    print_appropriate_error_message();	
  };
  
When catch is called, where can I go to retrive the information about why an exception was thrown? Thanx,

[Hugo Ferreira][Positronic Dreams][Stick Soldiers]
"Please Review my WebSite!".

Share this post


Link to post
Share on other sites
Catching things like divide by zero and access violations is a compiler-specific issue. If you''re using VC++, you can use the _set_se_translator to do it. Look it up in MSDN...

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
catch can "catch" any type of variable or even a user defined type. Just catch(int) if it was an int that was thrown or
catch(MyClass)if you threw a MyClass. I believe vc6 had its own error types too for things like divide by zero or a failed call to new but no idea what they were off the top of my head.

Share this post


Link to post
Share on other sites
You dont. If you want any information from the exception caught, you need to name it in the catch clause. With a catch(...) there is no way of getting additional information.
The preferrable way would be to ensure that all exceptions thrown in your code derive from a common base(std::exception would be the natural choice) and catch that.



"When I look upon seamen, men of physical science, and philosophers, man is the wisest of all beings. When I look upon priests, prophets, and interpreters of dreams, nothing is so contemptible as man."


  Diogenes

Share this post


Link to post
Share on other sites
You don''t, it''s not possible.

What you can do, is add catches for specific types beforehand, like so:


  
try
{
// try something

}
catch( ExceptionType1 e )
{
// handle exception type 1.

}
catch( ExceptionType2 e )
{
// handle exception type 2

}
catch( ... )
{
// handle every other type of exception.

}


In a way, the catch(...) is a lot like the default label in a switch statement.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by MadKeithV
You don''t, it''s not possible.



You can use runtime type information to identify the exception type. However, I have never seen a reason why you would want to do this over the normal method.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Arild Fines
Use RTTI on what? The <code>catch(...)</code> clause doesnt give you an object to query.



Use <code>catch (std::exception e)</code> and query that. It''s the best you can do if you want to use this approach; but I wouldn''t recommend it.

Share this post


Link to post
Share on other sites
Especially since you should catch a reference to avoid an unnecessary object copy.

Why on earth would you not recommend catching std::exception &x? I highly recommend it.

Share this post


Link to post
Share on other sites