Exceptions
Common exceptions
common_exceptions
#direction: right
#spacing: 10
#gravity: .8
#arrowSize: 4
#title: common_exceptions
[ArithmeticException|The exception that is thrown
for errors in an arithmetic,
casting, or conversion operation]
[Exception|Represents errors that
occur during application
execution.]
[RuntimeBinderException|Represents an error that occurs
when a dynamic bind in the C#
runtime binder is processed.]
[AggregateException|Represents one or more errors
that occur during application execution.]
[ApplicationException|Serves as the base class
for application-defined exceptions]
[JsonException|Defines a custom exception
object that is thrown when
invalid JSON text is encountered]
[SystemException|Serves as the base class
for system exceptions]
[AccessViolationException|An access violation occurs
in unmanaged or unsafe code when the code attempts
to read or write to memory that has not been allocated,
or to which it does not have access.
This usually occurs because a pointer has a bad value.]
[DivideByZeroException|The exception that is thrown
when there is an attempt to divide an
integral or Decimal value by zero.]
[OverflowException|The exception that is thrown
when an arithmetic, casting,
or conversion operation in a checked
context results in an overflow.]
[NotFiniteNumberException|The exception that is thrown
when a floating-point value is
positive infinity,
negative infinity,
or Not-a-Number (NaN).|
Available for programming languages
that do not support the concepts
of infinity and Not-a-Number
in floating-point operations.]
[FormatException|The exception that is thrown when
the format of an argument is invalid,
or when a composite format
string is not well formed.]
[StackOverflowException|The exception that is thrown when
the execution stack exceeds the stack size.
This class cannot be inherited.]
[InvalidCastException|The exception that is thrown
for invalid casting or explicit conversion.]
[InvalidOperationException|The exception that is thrown when a method
call is invalid for the object's current state.]
[OutOfMemoryException|The exception that is thrown when
there is not enough memory to continue
the execution of a program.]
[IOException|he exception that is thrown
when an I/O error occurs.]
[IndexOutOfRangeException|The exception that is thrown when
an attempt is made to access an element
of an array or collection with an
index that is outside its bounds.]
[DirectoryNotFoundException|The exception that is thrown
when part of a file or directory
cannot be found.]
[DriveNotFoundException|The exception that is thrown
when trying to access a drive
or share that is not available.]
[EndOfStreamException|The exception that is thrown
when reading is attempted
past the end of a stream.]
[FileLoadException|The exception that is thrown
when a managed assembly is found
but cannot be loaded.]
[FileNotFoundException|The exception that is thrown
when an attempt to access a file that
doesnot exist on disk fails.]
[NullReferenceException|The exception that is thrown when there
is an attempt to dereference
a null object reference.]
[PathTooLongException|The exception that is thrown when
a path or fully qualified file name is longer
than the system-defined maximum length.]
[NotImplementedException|The exception that is thrown
when a requested method or
operation is not implemented.]
[NotSupportedException|The exception that is thrown when
an invoked method is not supported,
or when there is an attempt to read,
seek, or write to a stream that does
not support the invoked functionality.]
[PlatformNotSupportedException|The exception that is thrown
when a feature does not run
on a particular platform.]
[UnauthorizedAccessException|
The exception that is thrown when
the operating system denies
access because of an I/O error
or a specific type of security error.]
[Exception] <:- [RuntimeBinderException]
[Exception] <:- [SystemException]
[Exception] <:- [AggregateException]
[Exception] <:- [ApplicationException]
[Exception] <:- [JsonException]
[SystemException] <:- [ArithmeticException]
[SystemException] <:- [AccessViolationException]
[SystemException] <:- [FormatException]
[SystemException] <:- [InvalidOperationException]
[SystemException] <:- [StackOverflowException]
[SystemException] <:- [OutOfMemoryException]
[SystemException] <:- [IOException]
[SystemException] <:- [IndexOutOfRangeException]
[SystemException] <:- [NullReferenceException]
[SystemException] <:- [NotImplementedException]
[SystemException] <:- [NotSupportedException] <:- [PlatformNotSupportedException]
[SystemException] <:- [UnauthorizedAccessException]
[IOException] <:- [DirectoryNotFoundException]
[IOException] <:- [DriveNotFoundException]
[IOException] <:- [EndOfStreamException]
[IOException] <:- [FileLoadException]
[IOException] <:- [FileNotFoundException]
[IOException] <:- [PathTooLongException]
[ArithmeticException] <:- [DivideByZeroException]
[ArithmeticException] <:- [NotFiniteNumberException]
[ArithmeticException] <:- [InvalidCastException]
[ArithmeticException] <:- [OverflowException]
ArithmeticException
The exception that is thrown
for errors in an arithmetic,
casting, or conversion operation
Exception
Represents errors that
occur during application
execution.
RuntimeBinderException
Represents an error that occurs
when a dynamic bind in the C#
runtime binder is processed.
AggregateException
Represents one or more errors
that occur during application execution.
ApplicationException
Serves as the base class
for application-defined exceptions
JsonException
Defines a custom exception
object that is thrown when
invalid JSON text is encountered
SystemException
Serves as the base class
for system exceptions
AccessViolationException
An access violation occurs
in unmanaged or unsafe code when the code attempts
to read or write to memory that has not been allocated,
or to which it does not have access.
This usually occurs because a pointer has a bad value.
DivideByZeroException
The exception that is thrown
when there is an attempt to divide an
integral or Decimal value by zero.
OverflowException
The exception that is thrown
when an arithmetic, casting,
or conversion operation in a checked
context results in an overflow.
NotFiniteNumberException
The exception that is thrown
when a floating-point value is
positive infinity,
negative infinity,
or Not-a-Number (NaN).
Available for programming languages
that do not support the concepts
of infinity and Not-a-Number
in floating-point operations.
FormatException
The exception that is thrown when
the format of an argument is invalid,
or when a composite format
string is not well formed.
StackOverflowException
The exception that is thrown when
the execution stack exceeds the stack size.
This class cannot be inherited.
InvalidCastException
The exception that is thrown
for invalid casting or explicit conversion.
InvalidOperationException
The exception that is thrown when a method
call is invalid for the object&
apos
s current state.
OutOfMemoryException
The exception that is thrown when
there is not enough memory to continue
the execution of a program.
IOException
he exception that is thrown
when an I/O error occurs.
IndexOutOfRangeException
The exception that is thrown when
an attempt is made to access an element
of an array or collection with an
index that is outside its bounds.
DirectoryNotFoundException
The exception that is thrown
when part of a file or directory
cannot be found.
DriveNotFoundException
The exception that is thrown
when trying to access a drive
or share that is not available.
EndOfStreamException
The exception that is thrown
when reading is attempted
past the end of a stream.
FileLoadException
The exception that is thrown
when a managed assembly is found
but cannot be loaded.
FileNotFoundException
The exception that is thrown
when an attempt to access a file that
doesnot exist on disk fails.
NullReferenceException
The exception that is thrown when there
is an attempt to dereference
a null object reference.
PathTooLongException
The exception that is thrown when
a path or fully qualified file name is longer
than the system-defined maximum length.
NotImplementedException
The exception that is thrown
when a requested method or
operation is not implemented.
NotSupportedException
The exception that is thrown when
an invoked method is not supported,
or when there is an attempt to read,
seek, or write to a stream that does
not support the invoked functionality.
PlatformNotSupportedException
The exception that is thrown
when a feature does not run
on a particular platform.
UnauthorizedAccessException
The exception that is thrown when
the operating system denies
access because of an I/O error
or a specific type of security error.
Common Exceptions
Implementing an own Exception
Every exception class should have 3 constructors for the following scenarios:
A parameterless constructor
A constructor that takes a string message
A constructor that takes a string message and an inner exception
The constructors should call the base class constructors with the appropriate parameters. Exceptions should be serializable for compatibility with older .NET Framework versions.
[Serializable]
public class CustomException : Exception
{
public CustomException()
{
}
public CustomException(string? message) : base(message)
{
}
public CustomException(string? message, Exception? innerException)
: base(message, innerException)
{
}
}
UnreachableExecption
UnreachableException indicates that a code path that should never be reached has been reached. This is usefull, when dealing with enum values, that can be extended in the future. When handling enum values, it is a good practice to always have a default case, that throws an exception. This way, the method will fail fast, if a new enum value is added, and the switch statement is not updated.
public enum MyEnum
{
First,
Second
}
public void SomeMethod(MyEnum value)
{
switch(value)
{
case MyEnum.First:
Console.WriteLine("First");
break;
case MyEnum.Second:
Console.WriteLine("Second");
break;
default:
throw new UnreachableException($"Unknown: {nameof(value)} value: {value}");
}
}