Exception in .NET


What is an Exception in .NET?

Exceptions are errors that occur during the runtime of a program.
The advantage of using exceptions is that the program doesn’t terminate due to the occurrence of the exception.
Whenever an exception is occurred the .NET runtime throws an object of specified type of Exception.
The class ‘Exception’ is the base class of all the exceptions.
Here are a few common types of exceptions:
  • ArgumentException
  • ArgumentNullException
  • ArgumentOutOfRangeException
  • DuplicateWaitObjectException
  • ArithmeticException
  • DivideByZeroException
  • OverflowException
  • NotFiniteNumberException
  • ArrayTypeMismatchException
  • ExecutionEngineException
  • FormatException
  • IndexOutOfRangeException
  • InvalidCastException
  • InvalidOperationException
  • ObjectDisposedException
  • InvalidProgramException
  • IOIOException
  • IODirectoryNotFoundException
  • IOEndOfStreamException
  • IOFileLoadException
  • IOFileNotFoundException
  • IOPathTooLongException
  • NotImplementedException
  • NotSupportedException
  • NullReferenceException
  • OutOfMemoryException
  • RankException
  • SecuritySecurityException
  • SecurityVerificationException
  • StackOverflowException
  • ThreadingSynchronizationLockException
  • ThreadingThreadAbortException
  • ThreadingThreadStateException
  • TypeInitializationException
  • UnauthorizedAccessException
It is a runtime error which occurs because of unexpected and invalid code execution.
.Net had enhanced exception handling features. All exceptions inherit from System.Exception.

Explain how to Handle Exceptions in .NET 2.0.

The different methods of handling the exceptions are:
  • 1.
    try
    {
         // code
    }
    catch(Exceptiontype *etype_object)
    {
         // code
    }
  • 2.
    try
    {
           // code
    }
    catch(Exceptiontype *etype_object)
    {
            throw new Custom_Exception();
    }
Exceptions should never be handled by catching the general System.Exception errors, rather specific exceptions should be caught and handled.
They are handled using try catch and finally. Finally is used to cleanup code as it's always executed irrespective of whether an exception has occurred or not. E.g.
try
{
      FileInfo file=new FileInfo(@"c:\abc.txt")
}
catch(FileNotFoundException e)
{
      //handle exception
}
Finally
{
         //cleanup code here
}
If file is found, then finally will get invoked else both catch and finally will occur.

What are Custom Exceptions?

Custom Exceptions are user defined exceptions.
There are exceptions other than the predefined ones which need to be taken care of.
For example: The rules for the minimum balance in a Salary A/C would be different from that in a Savings A/C due to which these things need to be taken care of during the implementation.
Custom exception needs to derive from the System.Exception class. You can either derive directly from it or use an intermediate exception like SystemException or ApplicationException as base class. Custom exeptions are created to handle very specific exceptions and to provide more details about it.
We need custom exceptions to have better predictability of our application. By using custom exceptions we can throw and handle our own exceptions, providing a much more predictable and stable application.
public class MyCustomException : Exception
{
        public MyCustomException()
        : base()
        {
        }

        public MyCustomException(string Message)
        : base(Message)
        {
        }
        public MyCustomException(string Message, Exception InnerException)
        : base(Message, InnerException)
        {
        }
        protected MyCustomException(SerializationInfo Info, StreamingContext Context)
        : base(Info, Context)       
        {
        }
}

0 comments:

Post a Comment