The
exception is the process that occurred during the run time in the software
application, it happens when the developer does not manage the code in the
right way or something is not handled properly. an exception is certain types
and breaks the application. In this blog, I will show a few types of
exceptions, which we face in everyday development, in this blog my focus in.Net
C#, as per the language the name may be vary based on the syntax but the type
of exception should have the same for all languages
Exceptions Overview
Exceptions have the following properties:
- to use include exception need to import
System.Exception
. - always use
try
block around the statements. - Once the exception occurs,
try
block, the flow of control jumps to the associated exception handler. In C#, the catch
keyword is used to define an exception handler. - If an exception handler is not defined for an exception in the program, it stops executing with an error message.
- if you do not write any catch handler then leave the application in a known state. If you catch it, rethrow it using the
throw
keyword at the end of the catch
block, it will pass on it to the next handler. - we can add an exception defined variable to obtain more information about the exception that occurred.
- by using the throw keyword, we can be explicitly generated by a program.
System.Exception
.try
block around the statements.try
block, the flow of control jumps to the associated exception handler. In C#, the catch
keyword is used to define an exception handler.throw
keyword at the end of the catch
block, it will pass on it to the next handler.Types of Exception
Here are the details of a few types of exceptions, that exception is not only bound with C#, indeed it comes with every programing language only the syntax is changed
ArgumentException
ArrayTypeMismatchException
ArgumentNullException
ArgumentOutOfRangeException
DivideByZeroException
FileNotFoundException
FormatException
IOException
IndexOutOfRangeException
InvalidOperationException
InvalidCastException
KeyNotFoundException
NotSupportedException
NullReferenceException
OverflowException
OutOfMemoryException
StackOverflowException
TimeoutException
Here is a detailed description of all
ArgumentException
This is the special kind of exception that occurred when the
arguments are the data you pass into the method's parameters. it is the actual
value of this variable that gets passed to function. if the bed argument
passed (e.g., wrong data type) to the method then this exception occurred. to
overcome it always pass the argument with the defined data type
ArrayTypeMismatchException
This is a special kind of exception, It occurred when any
mismatch happened in the array type.
ArgumentNullException
This is a special kind of exception, It occurred when the
null argument is passed to a method.
ArgumentOutOfRangeException
This is a special kind of exception, It happens if the value
of an argument is outside the range of valid values.
DivideByZeroException
This is a special kind of exception, It happened when the
Integer value was divided by 0 (Zero)
FileNotFoundException
This is a special kind of exception, This exception occurred
if the physical file is not found in the specified location
FormatException
This is a special kind of exception, This exception
occurred when the specific format is not provided, eg., string value parses to
int it will occur.
IOException
This
is a special kind of exception, It occurred when I/O (Input-Output) related
operations happened and if throwing an exception due to some mismatch.
IndexOutOfRangeException
This
is a special kind of exception, It occurred when the index of an array is out
of range, eg.,
The first and last elements in the array are
It
throws the exception
InvalidOperationException
This
is a special kind of exception, It occurred when the method call is invalid in
an object's current state.
InvalidCastException
This
is a special kind of exception, This occurred when the typecasting was not done
appropriately for example if
int
convert to string, it throws the exception.
NotSupportedException
It occurred when a method or operation is not supported.
If
isReadOnly can only be set at the time when the object is created (e.g. a
constructor argument), and never at any other time, then NotSupportedException
should be used
NullReferenceException
This
kind of exception has occurred when the object has a null value.
OutOfMemoryException
This
Kind of exception occurred when the application works on a large amount of
memory processing and does not get enough memory to execute the code.
means it occurred when the system does not have enough memory space to perform
any operation.
one
of the examples is: it also happened when you are attempting to expand a
StringBuilder object beyond the length defined by its StringBuilder.
OverflowException
This
kind of exception occurred when arithmetic, casting, or conversion operation is
performed and results in an overflow.
one
of the examples is., When we set the value to int.Parse() method
that is out of integer range will through the Exception, refer below code
snippet
StackOverflowException
This kind of exception comes when a stack in memory
overflows. usually, it happened due to recursive methods call or an infinite
loop is running so that it breaks the current operation which is in progress
and through the exception.
TimeoutException
It
occurred when a very long process of the operation is taking place in the
application and exceed the threshold of the timeout,
In
simple words, it comes when the time interval allotted to an operation has
expired.
Conclusion
The
exception comes in any way and It is clear that the exception breaks the
application and it makes a loss of business and clients confidence to overcome
this issue, the developer should follow the guidelines of coding and use
try-catch properly to handle the exceptions. apart from that developers can
create their own custom exception which can handle the exception without
defining their type.
Hope it will help!!!
Kindly
share your feedback so that I can improve my blogs
Thank
you :)
Comments
Post a Comment