Complete List of System Exceptions in C#
C# provides a rich set of built-in exceptions under the System
namespace to handle various runtime errors. These exceptions help you identify and handle different error conditions in your code effectively.
Below is a comprehensive list of System exceptions in C#:
โ Commonly Used Exceptions
- ArgumentException โ Invalid argument passed to a method.
- ArgumentNullException โ Argument passed is
null
where it shouldn’t be. - ArgumentOutOfRangeException โ Argument value is out of acceptable range.
- ArithmeticException โ Arithmetic operation errors (like division by zero).
- ArrayTypeMismatchException โ Type mismatch while storing an element in an array.
- DivideByZeroException โ Attempt to divide a number by zero.
- FormatException โ Invalid format conversion (e.g., converting letters to a number).
- IndexOutOfRangeException โ Accessing an invalid index in arrays or collections.
- InvalidCastException โ Invalid type casting operation.
- InvalidOperationException โ Method call is invalid for the object’s current state.
- KeyNotFoundException โ Accessing a non-existent key in a dictionary.
- NotImplementedException โ Method or operation is not implemented.
- NullReferenceException โ Trying to access a member of a null object.
- ObjectDisposedException โ Accessing a disposed object.
- OutOfMemoryException โ Insufficient memory to continue execution.
- OverflowException โ Arithmetic operation exceeds data type limits.
- StackOverflowException โ Call stack limit exceeded (often due to recursion).
ย
๐ File and I/O Related Exceptions
- DirectoryNotFoundException โ Directory path is invalid or doesnโt exist.
- DriveNotFoundException โ Specified drive is unavailable.
- EndOfStreamException โ End of a stream is reached unexpectedly.
- FileLoadException โ Failure loading a file.
- FileNotFoundException โ Requested file not found.
- IOException โ General I/O operation errors.
- PathTooLongException โ File or directory path is too long.
ย
๐ Security and Permissions Exceptions
- SecurityException โ Security violation occurs.
- UnauthorizedAccessException โ Unauthorized access to a resource.
ย
๐งฎ Serialization and Reflection Exceptions
- InvalidProgramException โ Code generation error by the compiler.
- MemberAccessException โ Access to class members fails.
- MissingFieldException โ Referencing a non-existent field.
- MissingMethodException โ Referencing a non-existent method.
- NotSupportedException โ Operation not supported.
- ReflectionTypeLoadException โ Type loading error via reflection.
- TypeInitializationException โ Failure during static constructor execution.
- TypeLoadException โ Failure to load a specified type.
ย
๐ก๏ธ Threading and Task Exceptions
- OperationCanceledException โ Operation was canceled.
- ThreadAbortException โ Thread is being aborted.
- ThreadInterruptedException โ Interrupted while in a waiting state.
- TimeoutException โ Operation took too long to complete.
ย
๐๏ธ Other Important Exceptions
- AccessViolationException โ Unauthorized memory access.
- InsufficientExecutionStackException โ Insufficient stack for execution.
- PlatformNotSupportedException โ Operation not supported on the platform.
- RankException โ Array dimension mismatch during an operation.
- SynchronizationLockException โ Object synchronization error.
- UriFormatException โ Invalid URI format.
๐ Conclusion
Phew! That was quite the journey through the land of common exceptions in C#, wasnโt it? ๐ข We looked at how errors like NullReferenceException
, FileNotFoundException
, and ArgumentException
occurโand more importantlyโhow to handle them gracefully. ๐ช Remember, exceptions arenโt your enemy; theyโre helpful signals guiding you toward writing safer, more reliable code. ๐ก๏ธ
Hereโs the takeaway: always expect the unexpected. Things like user input errors, missing files, or invalid data will sneak in. With proper exception handling, your programs will be rock solid and user-friendly. Your future self (and your users) will thank you! ๐
ย
๐ฎ Next what?
So, whatโs next on this coding adventure? ๐ฏ In the upcoming chapter, weโll explore the checked and unchecked statements in Exception Handling. Curious about how to manage arithmetic overflows and avoid sneaky calculation errors? ๐ฏ Donโt miss outโitโs going to be eye-opening (and fun)! ๐
Stay curious, keep coding, and Iโll catch you in the next lesson! ๐๐ป