Custom Exceptions in C sharp Arabic #41

 


Custom Exceptions in C sharp Arabic #41

Suggested Videos

Inner Exception in C sharp Arabic #40

❑When to create custom exceptions.

❑Creating a custom exception from the scratch.

❑Throwing and catching the custom exception.



Custom Exception

To understand custom exceptions , you should have good understanding of Inheritance. Exception Handling Basics. Inner Exception.

When do you usually go for creating your own custom exceptions ?
If none of the already existing dotnet exceptions adequately describes the problem.

Example: 
1-I have an asp.net web application. 
2-The application should allow the user to have only one logged in session. 
3-If the  user is already logged in , and if he opens another browser window and tries to login again , the application should throw an error stating he is already logged in another browser window.

With in the .NET framework we don’t have any exception, that adequately describes this problem. So this scenario is one of the examples where you want to create a custom exception.




Custom Exception 

Create a class that derives from System . Exception class . As a convention, end the class name with Exception suffix . All.NET exceptions end with , exception suffix.

Provide a public constructor , that takes in a string parameter. This constructor simply passes the string parameter, to the base exception class constructor.

Using InnerExceptions, you can also track the  original exception. If you want to provide this capability for your custom exception class , then overload the constructor accordingly.

If you want your exception class object to work  across application domains, then the object must be serializable . To make your exception class serializable mark it with Serializable attribute and provide a constructor that invokes the base Exception class constructor that takes in serializationinfo and StreamingContext objects as parameters.

Note: it is also possible to provide your own custom serialization , Which will discuss in a later session.

Follow Us

YouTube

 If you want to download pptx : click Telegram

Facebook

Twitter

Instagram

Tik Tok





إرسال تعليق

أحدث أقدم