locked
Types of Exceptions in C# RRS feed

  • Question

  • Types of Exceptions in C#
    Thursday, November 19, 2015 10:17 AM

Answers

  • Hello There,

    Please find below the types of exceptions 

    System.IO.IOException
    System.IndexOutOfRangeException
    System.ArrayTypeMismatchException
    System.NullReferenceException
    System.DivideByZeroException
    System.InvalidCastException
    System.OutOfMemoryException
    System.StackOverflowException

    Cheers

    Dinesh

    • Proposed as answer by Dinesh Gabhane Thursday, November 19, 2015 11:36 AM
    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:00 AM
    Thursday, November 19, 2015 11:35 AM
  • Types of Exceptions in C#

    Something wrong with your google skills ;-) Try dotnet list of exceptions or similar.

    Alternative: In your code, type 'throw new exception'; at the moment that you're about halfway typing 'exception', you will have a whole context menu with mostly the available exceptions ;-)

    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:00 AM
    Thursday, November 19, 2015 12:02 PM
  • Hi,

    First of all you have to know what are exceptions and how to deal with it. 

    <copied>

    Exceptions are unforeseen errors that happen in your programs. Most of the time, you can, and should, detect and handle program errors in your code. For example, validating user input, checking for null objects, and verifying the values returned from methods are what you expect, are all examples of good standard error handling that you should be doing all the time.

    However, there are times when you don't know if an error will occur. For example, you can't predict when you'll receive a file I/O error, run out of system memory, or encounter a database error. These things are generally unlikely, but they could still happen and you want to be able to deal with them when they do occur. This is where exception handling comes in.

    When exceptions occur, they are said to be "thrown". What is actually thrown is an object that is derived from the System.Exception class. In the next section, I'll be explaining how thrown exceptions are handled with try/catch blocks.

    The System.Exception class provides several methods and properties for obtaining information on what went wrong. For example, the Message property provides summary information about what the error was, the Stacktrace property provides information from the stack for where the problem occurred, and the ToString() method is overridden to reveal a verbose description of the entire exception.

    Identifying the exceptions you'll need to handle depends on the routine you're writing. For example, if the routine opened a file with the System.IO.File.OpenRead() method, it could throw any of the following exceptions:

        SecurityException
        ArgumentException
        ArgumentNullException
        PathTooLongException
        DirectoryNotFoundException
        UnauthorizedAccessException
        FileNotFoundException
        NotSupportedException

    </copied>

    The System.Exception class provides several methods and properties for obtaining information on what went wrong.

    SystemException Class

    Exception Class

    Exception Hierarchy

    Thanks,

    Sabah Shariq

    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:00 AM
    Thursday, November 19, 2015 12:18 PM
  • Please refer to the following page for a list of built-in classes (exceptions) that inherits from System.Exception: https://mikevallotton.wordpress.com/2009/07/08/net-exceptions-all-of-them/

    Please refer to the following thread for more information about this:

    List of C# exceptions: https://social.msdn.microsoft.com/Forums/vstudio/en-US/3d594119-376d-4c29-b334-1896c980f80d/list-of-c-exceptions

    Also note that you can define custom exceptions yourself and they will of course not be listed among the built-in ones.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:00 AM
    Thursday, November 19, 2015 1:08 PM

All replies

  • Hello There,

    Please find below the types of exceptions 

    System.IO.IOException
    System.IndexOutOfRangeException
    System.ArrayTypeMismatchException
    System.NullReferenceException
    System.DivideByZeroException
    System.InvalidCastException
    System.OutOfMemoryException
    System.StackOverflowException

    Cheers

    Dinesh

    • Proposed as answer by Dinesh Gabhane Thursday, November 19, 2015 11:36 AM
    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:00 AM
    Thursday, November 19, 2015 11:35 AM
  • Types of Exceptions in C#

    Something wrong with your google skills ;-) Try dotnet list of exceptions or similar.

    Alternative: In your code, type 'throw new exception'; at the moment that you're about halfway typing 'exception', you will have a whole context menu with mostly the available exceptions ;-)

    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:00 AM
    Thursday, November 19, 2015 12:02 PM
  • Hi,

    First of all you have to know what are exceptions and how to deal with it. 

    <copied>

    Exceptions are unforeseen errors that happen in your programs. Most of the time, you can, and should, detect and handle program errors in your code. For example, validating user input, checking for null objects, and verifying the values returned from methods are what you expect, are all examples of good standard error handling that you should be doing all the time.

    However, there are times when you don't know if an error will occur. For example, you can't predict when you'll receive a file I/O error, run out of system memory, or encounter a database error. These things are generally unlikely, but they could still happen and you want to be able to deal with them when they do occur. This is where exception handling comes in.

    When exceptions occur, they are said to be "thrown". What is actually thrown is an object that is derived from the System.Exception class. In the next section, I'll be explaining how thrown exceptions are handled with try/catch blocks.

    The System.Exception class provides several methods and properties for obtaining information on what went wrong. For example, the Message property provides summary information about what the error was, the Stacktrace property provides information from the stack for where the problem occurred, and the ToString() method is overridden to reveal a verbose description of the entire exception.

    Identifying the exceptions you'll need to handle depends on the routine you're writing. For example, if the routine opened a file with the System.IO.File.OpenRead() method, it could throw any of the following exceptions:

        SecurityException
        ArgumentException
        ArgumentNullException
        PathTooLongException
        DirectoryNotFoundException
        UnauthorizedAccessException
        FileNotFoundException
        NotSupportedException

    </copied>

    The System.Exception class provides several methods and properties for obtaining information on what went wrong.

    SystemException Class

    Exception Class

    Exception Hierarchy

    Thanks,

    Sabah Shariq

    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:00 AM
    Thursday, November 19, 2015 12:18 PM
  • Please refer to the following page for a list of built-in classes (exceptions) that inherits from System.Exception: https://mikevallotton.wordpress.com/2009/07/08/net-exceptions-all-of-them/

    Please refer to the following thread for more information about this:

    List of C# exceptions: https://social.msdn.microsoft.com/Forums/vstudio/en-US/3d594119-376d-4c29-b334-1896c980f80d/list-of-c-exceptions

    Also note that you can define custom exceptions yourself and they will of course not be listed among the built-in ones.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by DotNet Wang Tuesday, December 1, 2015 1:00 AM
    Thursday, November 19, 2015 1:08 PM