Exceptions in C#

By | February 11, 2019

Exception -: Exception handling helps applications trap and respond in a predictable and robust manner to exceptional events. This enhances the correctness of an application, which naturally improves customer satisfaction.

Exception handling is an important ingredient of a robust application, but it is often an afterthought. We should proactively consider exception handling as an integral part of the application design and include it in all aspects of application planning and development. Conversely, treating exception handling as an afterthought leads to poorly implemented solutions for exception handling and a less robust application.

 Exceptions in C# provide a structured, uniform, and type-safe way of handling both system-level and application-level error conditions. The exception mechanism in C# is quite similar to that of C++, with a few important differences.

  •  C#, all exceptions must be represented by an instance of a class type derived from System.Exception. In C++, any value of any type can be used to represent an exception.
  • In C#, a finally block  can be used to write termination code that executes in both normal execution and exceptional conditions. Such code is difficult to write in C++ without duplicating code.
  • In C#, system-level exceptions such as overflow, divide-by-zero, and null deference have well-defined exception classes and are on a par with application-level error conditions.
What is an exception ?

Exceptions are events, often indicating an error, that alter the normal flow of execution within an application. Exceptions are categorized as system exceptions or application exceptions. System exceptions are raised by the Common Language Runtime (CLR) and include null-reference, out-of-memory, divide-by-zero, and stack overflow exceptions.

Some exceptional events are detected by application logic, not by the run time. Application exceptions are useful in these circumstances. Application exceptions, considered user-defined exceptions, are thrown by the application, not by the CLR. As an example, constructors that fail are not always detectable by the CLR. In addition, constructors implicitly return void, which prevents returning an error code. For these reasons, throwing an application exception in the failed constructor is the best solution to notify the application and user. as like  example

  using System;
  namespace Donis.CSharpBook {
  public class Starter {
             public static void Main() {
                                                            int var1 = 5, var2 = 0;
                                                             var1 /= var2; // exception occurs
                                                     }
                                      }
                           }

Reason of exception -:  There are two reason of exception.

  1. A Throw Statement
  2.  System Class statement

Throw Statement -: A throw statement  throws an exception immediately and unconditionally. Control never reaches the statement immediately following the throw.

Certain exceptional conditions that arise during the processing of C# statements and expression cause an exception in certain circumstances when the operation cannot be completed normally.

System.class exception -: The System.Exception class is the base type of all exceptions. This class has a few notable properties that all exceptions are..

  •  Message is a read-only property of type string that contains a human-readable description of the reason for the exception.
  • Inner-exception is a read-only property of type Exception. If its value is non-null, it refers to the exception that caused the current exception that is, the current exception was raised in a catch block handling the Inner-exception. Otherwise, its value is null, indicating that this exception was not caused by another exception. The number of exception objects chained together in this manner can be arbitrary.

 

There are some exception class are available as like here

 

exception

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *