Reading exception from try catch when exception type not specified?

You'll want to catch the exception type you care about. When you do, you'll have access to all the properties of that exception. Try { //Do my work!

} catch (MyExceptionType e) { string s = e. Message; } Here's a reference in MSDN, to get up to speed. Regarding your edit: no there is no way to access the thrown exception unless the exception is explicitly specified in your catch statement.

1. Beat me to it! :) – Mitch Wheat Apr 18 at 7:35 I'm curious about your use case, though, Maxim.

Care to elaborate? – Michael Petrotta Apr 18 at 7:47 No use case. Just curiosity about the language structure and specifications.

– Maxim Gershkovich Apr 18 at 8:11.

Yes, like this: try { //Do my work! } catch (mySpecificException myEx) { //Handle my exception } catch (Exception ex) { //Handle my exception } (Most specific to least specific).

No. Using the bare catch indicates that you do not care about the actual exception otherwise, why not use catch (System. Exception ex) to catch any exception?

Of course, you should only catch exceptions you will handle.

You need to indicate the specific type of exception that you are catching, and assign that to a variable. Do that using this syntax instead: try { // Do work } catch (MyUndefinedExceptionObject ex) { Debug. WriteLine(ex.

Message); } You can also include multiple catch blocks with the type of exception altered accordingly. However, remember that you should always order them from most derived to least derived, ending with the base class for all exceptions, System.Exception. You also should generally refrain from catching System.

Exception, instead preferring only to catch the derived exceptions that you can handle in the catch block and that won't corrupt your program's state. Catching System. Exception class is a bad idea because you'll also catch exceptions that you won't be able to handle, like an OutOfMemoryException or a StackOverflowException.

Microsoft has a helpful article on best practices here: Best Practices for Handling Exceptions.

You'll want to catch the exception type you care about. When you do, you'll have access to all the properties of that exception.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions