C# SystemException - C# Exception Handling



C# SystemException - C# Exception Handling

  • In C#, System Exception is a predefined exception class and it is used to handle system related exceptions.
  • It has various child classes like: Validation Exception, Arithmetic Exception, Data Exception, Argument Exception, Stack Overflow Exception, etc.
  • It works as base class for system exception namespace and it consists of rich constructors, properties and methods.

Syntax

[SerializableAttribute]  
[ComVisibleAttribute(true)]  
public class SystemException : Exception  

System Exception Constructor

  • System exception () is used to initialize a new instance of the System exception class.
  • Serialization info and Streaming context is used to initialize a new instance System exception class with serialization data.
  • String is used to initialize a new instance System exception class with a specified error message.
  • String and Exception is used to initialize a new instance System exception class with a specified error message a reference to the inner exception that is the cause of this exception.

System Exception Properties

  • Data is used to get a collection of key/value pairs that provide additional user-defined information about the exception.
  • Help link is used to get or set a link to the help file associated with this exception.
  • HResult is used to get or set HRESULT, a coded numerical value that is assigned to a specific exception.
  • Inner Exception is used to get the exception instance that caused the current exception.
  • Message is used to get a message that describes the current exception.
  • Source is used to get or set the name of the application that causes the error.
  • Stack Trace is used to get a string representation of the immediate frames on the call stack.
  • Target site is used to get the method that throws the current exception.

System Exception Methods

  • Equal method is used to check that the specified object is equal to the current object or not.
  • Finalize method is used to free resources and perform cleanup operations.
  • Get base Exception method is used to get root exception.
  • Get hash code is used to get hash code.
  • Get type is used to get the runtime type of the current instance.
  • Member wise clone is used to create a shallow copy of the current Object.
  • To string is used to create and return a string representation of the current exception.

Sample Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Csharp_Program
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int[] arr = new int[5];
                arr[10] = 25;
            }
            catch (SystemException e)
            {
                Console.WriteLine(e);
                Console.ReadLine();
            }
        }
    }
}

Output

custom-exception

Related Searches to C# SystemException - C# Exception Handling