Exception
, and not directly on the basis of Exception
, because it gives you the opportunity to define your own handler and separate the tracking and handling of exceptions thrown by your code and the code of .NET.Message
field. Despite the fact that the idea of ​​packing complex data in a Message
in the form of a JSON string is tempting, it is rarely a good idea because it adds an extra expense to the coding, localization, decoding.Exception
, by calling Exception.ToString();
this makes it easier to track when debugging exceptions. public class SimpleClass { public static string DoSomething() { try { return SimpleLibrary.ReportStatus(); } catch (Exception) { return "Failure 1"; } } } public class SimpleLibrary { public static string ReportStatus() { return String.Format("Success {0}.", 0); } }
SimpleClass
and SimpleLibrary
are in separate assemblies, then in the case when both assemblies are installed correctly, the code runs correctly, displaying the message “Success 0”, and if the assembly with the SimpleLibrary
class SimpleLibrary
not installed, then the code is executed incorrectly, displaying the message “Failure 1”, despite the fact that no error occurs during the execution of the ReportStatus
function. The problem is not obvious due to too generalized handling of exceptions. The code formatting the string throws ArgumentNullException
and FormatException
, so these exceptions should be caught in the catch block, then the reason for the error becomes obvious - this is a FileNotFoundException
exception due to the absence or incorrect installation of the assembly containing the SimpleLibrary
class. try { DoSomething(); } catch (SomeException) { // TODO: ... }
finally
block . Delete temporary objects in finally
blocks. Consider the operation of writing a temporary file. void WorkWithFile(string filename) { try { using (StreamWriter sw = new StreamWriter(filename)) { // TODO: Do something with temporary file } File.Delete(filename); } catch (Exception) { File.Delete(filename); throw; } }
finally
block. void WorkWithFile(string filename) { try { using (StreamWriter sw = new StreamWriter(filename)) { // TODO: Do something with temporary file } } finally { File.Delete(filename); } }
using
. Use the using
statement. It guarantees a call to the Dispose
method, even if an exception occurs in the object when the methods are called. using (Font f = new Font("Times New Roman", 12.0f)) { byte charset = f.GdiCharSet; }
using
statement is equivalent to a try/finally
block, but more compact and concise. Font f = new Font("Times New Roman", 12.0f); try { byte charset = f.GdiCharSet; } finally { if (f != null) ((IDisposable)f).Dispose(); }
null
if there is no resource. According to the convention common to the .NET API, functions should not throw exceptions when there is no resource; they should return null
. So GetManifestResourceStream
returns null
if resources were not specified during compilation or not visible to the calling code. try { // Do something to throw exception } catch (Exception e) { // Do something to handle exception throw e; // Wrong way! throw; // Right way }
throw e;
information about the place of generation of the exception will be replaced by a new line, since the creation of a new exception will clear the call stack. Instead, call throw;
which simply rethrows the original exception.Exception
serializable with [Serializable]
. This is useful because it is never known in advance where the exception will be received, for example, in a web service. [Serializable] public class SampleSerializableException : Exception { public SampleSerializableException() { } public SampleSerializableException(string message) : base(message) { } public SampleSerializableException(string message, Exception innerException) : base(message, innerException) { } protected SampleSerializableException(SerializationInfo info, StreamingContext context) : base(info, context) { } }
NewException
these constructors are:public NewException();
public NewException(string);
public NewException(string, Exception);
protected or private NewException(SerializationInfo, StreamingContext);
Source: https://habr.com/ru/post/178805/
All Articles