Best Practice Exclusions

I would like to know which way to make an exception, because I have a lot of checks inside my Try statement, and if I get an Exception there, my Catch statement can tell me what will happen, but how can I find out in which area the Exception occurs?

Code example

 try { // If I get a Exception when converting to number, // I will understand the error // but how could I know where in my `Try` statement was the error ? int valor = Convert.ToInt32(xmlnode[i].ChildNodes.Item(2).InnerText.Trim()); // A Lot of another validations here } Catch(Exception e) { this.LogInformation(e.Message); } 
+4
source share
6 answers

Best practice would be to not use Try-Catch at all when converting strings to numbers. Therefore, you should use TryParse methods such as int.TryParse .

 // note that here is also a possible error-source string valorToken = xmlnode[i].ChildNodes.Item(2).InnerText.Trim(); int valor; if(!int.TryParse(valorToken, out valor)) { // log this } // else valor was parsed correctly 

In addition, if you want to provide accurate error messages, you must use multiple Try-Catch or handle different types of exceptions (the most common type of Exception should be the last).

+3
source

Do not use Convert.ToInt32 if you are not sure about the value. Use Int32.TryParse instead:

 int valor; if (Int32.TryParse(xmlnode[i].ChildNodes.Item(2).InnerText.Trim(), out valor)) { // Worked! valor contains value } else { // Not a valid Int32 } 

In addition, you should not use Exceptions to verify validation errors. Your verification code should calculate if this value is correct, and not crash if it is not. The validation class should expect to receive both valid and invalid data as input. Since you expect invalid input, you should not catch exceptions when they are invalid.

Come up with a test that validates the data and returns true or false. Almost all numeric types have a TryParse method, as described above. For your custom rules for other validation methods, come up with a specification that precisely defines what is valid and invalid input, and then write a method to implement this specification.

+3
source

Move try..catch inside the loop. This way you will know which element exactly caused the exception.

 foreach(var xmlNode in nodes) { try { // int valor = Convert.ToInt32(xmlNode.ChildNodes.Item(2).InnerText.Trim()); // A Lot of another validations here } catch(Exception e) { LogInformation(e.Message); // current item is xmlNode return; } } 
+1
source

If there is even the most distant possibility that the value you are trying to parse will not be collapsible, then this is not an exceptional circumstance. should not be considered an exception.

In this case, TryParse exists, which allows you to determine that the value is not valid for parsing:

 int valor; if(int.TryParse(xmlnode[i].ChildNodes.Item(2).InnerText.Trim(), out valor)) { // "valor" is sucessfully parsed } else { // invalid parse - do something with that knowledge } 
+1
source

If its various Exceptions that are thrown (i.e. different classes), then you will have to handle this with different catch attempts.

Usually you can:

 try { // If I get a Exception when converting to number, // I will understand the error // but how could I know where in my `Try` statement was the error ? int valor = Convert.ToInt32(xmlnode[i].ChildNodes.Item(2).InnerText.Trim()); // A Lot of another validations here } Catch(IOException ioe) { // Handle, log } Catch(ArgumentNullException ane) { // Handle, log } Catch(Exception e) { // Handle, log and potentially rethrow } 

You can also have individual catch attempts (like what, in my opinion, most people have), or nested catch attempts in your try block:

how

 // First block try { // Convert here once } catch (Exception ex) { // Handle and log } // Second block try { // Convert here once } catch (Exception ex) { // Handle and log } 

Not sure if this helps at all.

0
source
 try { } catch (Exception ex) { var stackTrace = new StackTrace(ex, true); var frame = stackTrace.GetFrame(0); var line = frame.GetFileLineNumber(); var method = frame.GetMethod(); } 
0
source

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


All Articles