Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are industry standard best practices for implementing custom exceptions in C#?

What are industry standard best practices for implementing custom exceptions in C#?

I have checked Google and there's a great number of recommendations, however I don't know which ones hold more credibility.

If anybody has any links to authoritative articles, that would also be helpful.

like image 898
Darren Young Avatar asked Jan 25 '11 09:01

Darren Young


People also ask

How is custom exception implemented?

You should only implement a custom exception if it provides a benefit compared to Java's standard exceptions. The class name of your exception should end with Exception. If an API method specifies an exception, the exception class becomes part of the API, and you need to document it.

How do you handle custom exceptions?

Basically, Java custom exceptions are used to customize the exception according to user need. Consider the example 1 in which InvalidAgeException class extends the Exception class. Using the custom exception, we can have your own exception and message. Here, we have passed a string to the constructor of superclass i.e.


2 Answers

The standard for creating custom exceptions is to derive from Exception. You can then introduce your own properties/methods and overloaded constructors (if applicable).

Here is a basic example of a custom ConnectionFailedException which takes in an extra parameter which is specific to the type of exception.

[Serializable] public class ConnectionFailedException : Exception {     public ConnectionFailedException(string message, string connectionString)         : base(message)     {         ConnectionString = connectionString;     }      public string ConnectionString { get; private set; } } 

In the application this could be used in scenarios where the application is attempting to connect to a database e.g.

try {     ConnectToDb(AConnString); } catch (Exception ex) {     throw new ConnectionFailedException(ex.Message, AConnString); } 

It's up to you to then handle the ConnectionFailedException at a higher level (if applicable)

Also have a look at Designing Custom Exceptions and Custom Exceptions

like image 198
James Avatar answered Sep 24 '22 10:09

James


Here is the code to create a custom exception:

using System; using System.Runtime.Serialization;  namespace YourNamespaceHere {     [Serializable()]     public class YourCustomException : Exception, ISerializable     {         public YourCustomException() : base() { }         public YourCustomException(string message) : base(message) { }         public YourCustomException(string message, System.Exception inner) : base(message, inner) { }         public YourCustomException(SerializationInfo info, StreamingContext context) : base(info, context) { }     } } 

See also: http://www.capprime.com/software_development_weblog/2005/06/16/CreatingACustomExceptionClassInC.aspx

like image 31
Michael Maddox Avatar answered Sep 24 '22 10:09

Michael Maddox