0% found this document useful (0 votes)
6 views22 pages

Exception

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

Exception

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

EXCEPTION

BUILT-IN EXCEPTION CLASSES IN C#

 C# .NET includes built-in exception classes for every possible error. The Exception
class is the base class of all the exception classes.
In the above figure, the Exception class
is the base class of the SystemException
and ApplicationException classes. The
SystemException class is the base class
for all the built-in exception classes
in .NET Framework.
 The ApplicationException was recommended to be base
class for all your custom exceptions classes (The custom
exeception class should be created if non of the system
exception classes can be used and you need new exception
class for business rule violations or for other application
related errors). It was meant to differentiates between
exceptions defined by applications versus exceptions
defined by the system. However, Microsoft now
recommends to derive custom exception classes from the
Exception class rather than the ApplicationException class .
 The following figure shows how the NullReferenceException is
thrown in Visual Studio debug mode when you access a null
object property at runtime.
BUILT-IN EXCEPTION CLASSES
EXCEPTION HANDLING IN C#
 Exceptions in the application must be handled to prevent crashing of the
program and unexpected result, log exceptions and continue with other
functionalities. C# provides built-in support to handle the exception using
try, catch & finally blocks.
 try block: Any suspected code that may raise
exceptions should be put inside a try{ } block.
During the execution, if an exception occurs, the
flow of the control jumps to the first matching
catch block.
 catch block: The catch block is an exception
handler block where you can perform some action
such as logging and auditing an exception. The
catch block takes a parameter of an exception
type using which you can get the details of an
exception.
 finally block: The finally block will always be
executed whether an exception raised or not.
Usually, a finally block should be used to release
resources, e.g., to close any stream or file objects
that were opened in the try block.
The following may
throw an
exception if you
enter a non-
numeric character.
TO HANDLE THE POSSIBLE EXCEPTIONS IN THE ABOVE EXAMPLE, WRAP
THE CODE INSIDE A TRY BLOCK AND HANDLE THE EXCEPTION IN THE
CATCH BLOCK, AS SHOWN BELOW.
 In the above example, we wrapped this code
inside a try block. If an exception occurs inside a
try block, then the program will jump to the catch
block. Inside a catch block, we display a message
to instruct the user about his mistake, and in the
finally block, we display a message about what to
do after running a program.
 A try block must be followed by catch or
finally or both blocks. The try block without a
catch or finally block will give a compile-time
error.
IDEALLY, A CATCH BLOCK SHOULD INCLUDE A PARAMETER OF A BUILT-IN
OR CUSTOM EXCEPTION CLASS TO GET AN ERROR DETAIL. THE
FOLLOWING INCLUDES THE EXCEPTION TYPE PARAMETER THAT
CATCHES ALL TYPES OF EXCEPTIONS.
EXCEPTION FILTERS

 You can use multiple catch blocks with the


different exception type parameters. This is called
exception filters. Exception filters are useful when
you want to handle different types of exceptions
in different ways.
 In this example, we have
specified multiple catch
blocks with different
exception types. We can
display an appropriate
message to the user,
depending upon the
error, so the user does
not repeat the same
mistake again.
INVALID CATCH BLOCK

 A parameterless catch block and a catch block with the Exception


parameter are not allowed in the same try-catch statements,
because they both do the same thing.
 Also, parameterless catch
block catch{ } or general
catch block
catch(Exception ex){ }
must be the last block. The
compiler will give an error
if you have other catch
blocks after a catch{ } or
catch(Exception ex) block.
FINALLY BLOCK

 The finally block is an


optional block and should
come after a try or catch
block. The finally block will
always be executed whether
or not an exception
occurred. The finally block
generally used for cleaning-
up code e.g., disposing of
unmanaged objects.
NESTED TRY-CATCH

 C# allows nested try-


catch blocks. When using
nested try-catch blocks,
an exception will be
caught in the first
matching catch block that
follows the try block
where an exception
occurred.
 An inner catch block will be executed in the
above example because it is the first catch
block that handles all exception types.

 If there isn't an inner catch block that


matches with raised exception type, then
the control will flow to the outer catch
block until it finds an appropriate exception
filter. Consider the following example.

You might also like