How to find out possible exceptions when using try catch?

According to MSDN , it is bad practice to catch exceptions without a specific type and use, for example, System.Net.Exception

Do I have to delve into the msdn manual to see the possible types of exceptions every time I am going to catch an error. Or is there some way in the IDE so I can see it quickly.

I am currently using Visual Studio 2013 Express Edition

  try { using (WebClient goog = new WebClient()) { goog.DownloadString("http://google.com"); } } catch(Exception E) { saveLog("methodname", E.Message); } 

EDIT: In this link , it looks like VS already has the ability to display exceptions, however, when I select a method, it only displays the type and parameters of the method. But he shows no exceptions

+5
source share
3 answers

The best practice is usually to only add handling for the exceptions that you expect during the execution time of your program.

If you are dealing with files, for example, handling types *** NotFoundException makes sense. Proper coding ensures that things like ArgumentNullException are not thrown, so they don't need processing, etc.

+2
source

Unlike Java, C # does not need to list your potential exceptions in the signature of your methods. It has some good sides and some bad sides. You just encountered one of the bad sides.

You cannot know which exception can be thrown if

  • The method you call is well documented with its possible exceptions (best option)
  • You know the specific bad cases, run them and see what exceptions they create (bad case).
  • You have no idea what could go wrong, and write down everything, modifying your catch every time something unexpected happens (worst case).
+1
source

There is no built-in function to show this automatically, but you put the caret somewhere in the method name and press CTRL and Space . The information shown here will be the same as in your link, so it should contain two exceptions for the DownloadString method.

Hovering over DownloadString will not necessarily show you the same information as clicking on a method name and pressing CTRL and Space (the latter shows you the exceptions thrown by the method).

0
source

Source: https://habr.com/ru/post/1202012/


All Articles