SlideShare a Scribd company logo
M. SC. (Computer Science)
Course Code : 23CsCmpP121
Course Name : DOT NET
PresentedBy : Priyanka Jadhav
Modern College of Arts,Science and Commerce ,Pune-5
Notes
Chapter 2
Introduction to C#
i.
Chapter 2
Introduction to C#
2.1.3 Functions, Delegates
Delegates
What is delegate?
In C#, delegate is a reference to the method. It works
like function pointer in C and C++. But it is objected-
oriented, secured and type-safe than function pointer.
For static method, delegate encapsulates method only.
But for instance method, it encapsulates method and
instance both.
The best use of delegate is to use as event.
Internally a delegate declaration defines a class which is the
derived class of System.Delegate.
i.
Chapter 2
Introduction to C#
2.1.3 Functions, Delegates
Delegates
A delegate is similar to a class .you can create an
instance of it, and when you do so, you pass in the
function name as a parameter to the delegate
constructor, and it is to this function the delegate will
point to .
Tip to remember delegate syntax -:
delegate syntax look very much similar to a
method with a delegate keyword.
i.
Chapter 2
Introduction to C#
2.1.3 Functions, Delegates
Delegates
Declaring Delegates
 Delegates in c# are declared using the delegate
keyword followed by the return type and
parameter of the referenced method.
 Declaring a delegate is quite similar to declaring
a method except that there is no implementation.
Thus , the declaration statement must end with a
semicolon.
public delegate int Calculation(int num1,int num2);
i.
Chapter 2
Introduction to C#
2.1.3 Functions, Delegates
Delegates
Note:-if the delegate is declared outside the class, you
cannot declare another delegate with the same name in
that namespace.
Delegates are called as type safe function
pointer.
i.
Chapter 2
Introduction to C#
2.1.3 Functions, Delegates
Delegates
Types of Delegate:-
1. Single delegate
2. Multicast delegate
3. Generic delegate
i.
Chapter 2
Introduction to C#
2.1.3 Functions, Delegates
Delegates
1. Single delegate:-
 Single cast delegate can be used to
invoke a single method.
 Single cast delegate point to single
method at a time.
 In this the delegate is assigned to a
single method at a time.
i.
Chapter 2
Introduction to C#
2.1.3 Functions, Delegates
Delegates
2. MultiCast Delegate:-
 A Multicast delegate is a delegate
that has references to more than one
function.
 When you invoke a multicast
delegate, all the functions the
delegate is pointing to, are invoked.
i.
Chapter 2
Introduction to C#
2.1.3 Functions, Delegates
There are 2 approaches to create a multicast delegate,
depending on the approach you use :-
 + or += to register a method with the delegate (adding
new method on existing delegate instance)
 -or -= to un-register a method with the delegate .
( can be used to remove a method from a delegate
instance. )
Note-A multicast delegate, invokes the methods in the
invocation list, in the same order in which they are
added.
Delegates
to be cont…
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Error Handling
Error handling is the process of responding to and
recovering from error conditions in your program.
The errors your program will encounter can be classified in
two categories:-
1. Syntax Errors
2. Logic Errors
to be cont…
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Error Handling
Syntax Errors
Syntax Errors occur in program due to poor understanding
of language syntax or mistake while typing.
Few examples of syntax errors in program are missing
semicolon at the end of statement, using reserved
errors as user defined identifiers ,using variable
without declaration etc.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Error Handling
Logic Errors
Logic Errors occur due to improper implementation
of program logic. Compiler does not show any logic
error. If there is any logic error in the program then
program gives wrong results. Logic errors are difficult
to spot.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Debugging
Debugging is the routine process of locating and
removing computer program bugs, errors or
abnormalities, which is methodically handled by
software programmers via debugging tools.
The Debugging is the program you used to debug
your code . The code or application that your are
debugging is called debugger.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Exception Handling is abnormal events that prevent a certain
task from being completed successfully.
Or in other word - It is process of responding to unwanted or
unexpected events when a computer program runs.
Exceptions are run-time errors that may cause a program to be
immediately terminated. Exception handling is a process of
handling these run-time errors. Handling an exception refers to
the action to be taken when an error occurs in order to save the
program from being prematurely terminated.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
For example:-consider a c# application that is
currently being executed. Assume that at some point
of time, the common language run-time(CLR)
discovers they it does not have the read permission to
read a particular file, the CLR immediately stops
further processing of the program and terminates its
execution abruptly. To avoid this from happening, you
can use the exception handling features of c#.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
C# can handle different types of exceptions using
exception handling statements. It allows you to handle
basically two kinds of exceptions.
These are:
System Defined Exceptions
User Defined Exceptions
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
The System.Exception class is the base class that allows you to
handle all exception c#. This means that all exception in c# inherit
the System.Exception class either directly or indirectly.
The system.Exception class contains public and protected method
that can be inherited by other exception class. In addition, the
System.
Exception class contains properties that are common to all
exceptions. The table shown alongside describe these properties.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Properties of Exception Class
Properties Descriptions
Message Display a message which indicates the
reason for the execution.
Source Provides the name of the application or the
object that caused the exception.
StackTrace Provides exception details on the stack at
the time the exception was thrown.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Exceptions Description
System.ArithmeticException This expt. Is thrown for problems that occur
due to arithmetic or casting and conversion
operations.
System.ArgumentException This expt. Is thorn when one of the arguments
does not match the parameter specifications
of the invoked method.
System.ArrayTypeMismatchException This exception is thrown when an attempt is
made to store data in an array whose type is
incompatible with the type of the array.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Exceptions Description
System.DvideByZeroException
System.IndexOutOfRangeException
System.InvalidCastException
System.ArgumentNullExcption
This exception is thrown when an attempt is
made to divide a numeric value by zero.
This exception is thrown when an attempt is
made to store data in an array using an
index that is less than zero or outside the
upper bound of the array.
This exception is thrown when an explicit
conversion from the base type or interface
type to another type fails.
This exception is thrown when a null
reference is passed to an argument of
method that does not accept null values.
System.NullReferenceException The exception is thrown when you try to
assign a value to a null object.
System.OutOfMemoryException This exception is thrown when there is not
enough memory to allocate to an object.
System.OverflowException This exception. Is thrown when the result of
an ariththmetic,casting or conversion
operation is too large to be stored in the
destination object or variable.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Exceptions Description
System.StackOverflowException This exception is thrown when the stack
runs out of space due to having too many
pending method calls.
System.data.DataException This exception Is thrown when errors are
generated while using the ADO.NET
components.
System.FormatException This exception is thrown when the format of
an argument does not match the format of
the parameter data type of the invoked
method.
System.OverflowException This exception. Is thrown when the result of
an ariththmetic,casting or conversion
operation is too large to be stored in the
destination object or variable.
Syste.IO.IOEXception This exception is thrown when any I/O error
occurs while accessing information using
streams , files and directories.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Catching Exceptions:-
Consider a group of boys playing throw ball wherein one boy throws
a ball and the other boys catch the ball. If any of the boys fail to
catch the ball, the game will be terminated. Thus the game does on
till the boys are successful in catching the ball in time.
Similarly, in c# exception that occur while working on particular
program need to be caught by exception handlers. If the program
does not contain the appropriate exception handler , then the
program might be terminated.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Catching Exceptions:-
Exception handling is implemented using try-catch construct in c#. This
construct consist of two blocks, the try block and the catch block.. The try
block encloses statements that might generate exceptions. When these
exceptions are thrown, the required actions are perform using the catch
block. Thus the catch block consist of the appropriate error-handlers that
handle exception.
The catch block follows the try block and may or may not contain
parameters. If the catch block does not contain any parameter, it can catch
any type of exception, if the catch block contains a parameter , it catches
the type of exception specified by the parameter.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
The following syntax is used for handling errors using the try and
catch blocks.
try
{
//statements that might raise exception or program code
}
catch(Exceptionclass objException)
{
//statements for handling the exception
}
Note:-the catch block is only executed if the try block throws an
exception.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
The “throw” Statement:-
The throw statement in c# allows you to programmatically throw
exceptions using the throw keyword.
The throw statement takes an instance of the particular exception
class as a parameter. However , if the instance does not refer to the
valid exception class, the c# compiler generates an error.
When you an exception using the throw keyword, the exception is
handled by the catch block.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
The “finally” Statement:-
In general, if any of the statements in the try block raises an
exception, the catch block is executed and the rest of the statement
in the try block are ignored. But , sometimes it becomes mandatory
to execute some statements irrespective of whether an exception is
raised or not. In such cases, a finally block is used.
Examples of action that can be placed in a finally block are: closing a
file, assigning object to null, closing a database connection and so
forth.
The finally block is an optional block appears after the catch block.
It enclose statements that are executed regardless of whether an
exception occurs.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Nested “try” blocks
The nested try block consists of multiple try-catch constructs. A
nested try block starts with a try block, which is called the outer try
block. This outer try block contains multiple try blocks within it,
which are called inner try blocks. If an exception is thrown by a
nested try block, the control passes to its corresponding nested
catch block.
consider an outer try block containing a nested try-catch –finally
construct. If the inner try block throws an exception, control is
passed to the inner catch block, however, if the inner catch block
does not contain the appropriate error handler, the control is passed
to the out catch block.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Nested “try” blocks
The following syntax is used to create nested try-catch blocks.
try
{
//outer try block
try
{
//inner try block
}
catch
{
//inner catch block
}
//this is optional
finally
{
//inner finally block
}
}
catch
{
//outer catch block
}
}
Chapter 2
Introduction to C#

More Related Content

Similar to Unit2_2.pptx Chapter 2 Introduction to C# (20)

PDF
Intake 37 5
Mahmoud Ouf
 
PPTX
Chapter_4_WP_with_C#_Exception_Handling_student_1.0.pptx
ZachariahAbera
 
PPT
12 Exceptions handling
maznabili
 
PPT
C# Exceptions Handling
sharqiyem
 
PPT
Exceptions
motthu18
 
PDF
Intake 38 5
Mahmoud Ouf
 
PPTX
12. Exception Handling
Intro C# Book
 
PPT
Exceptions
DeepikaT13
 
PPTX
Production Debugging at Code Camp Philly
Brian Lyttle
 
PPTX
Exception Handlin g C#.pptx
R S Anu Prabha
 
PPTX
Exception Handling in C#
Abid Kohistani
 
PPTX
Exception guidelines in c#
Ahmed Helmy
 
PPTX
C Sharp Tutorial : C Sharp Exception
Courseing Online
 
PPS
10 iec t1_s1_oo_ps_session_14
Niit Care
 
PDF
Csharp_Chap04
Mohamed Krar
 
PPTX
Week 2: Getting Your Hands Dirty – Part 2
Jamshid Hashimi
 
PPTX
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
DOCX
C sharp syllabus
deepa lakshmi
 
PPTX
Exception handling in .net
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
IakakkakjabbhjajjjjjajjajwsjException.pptx
KingDietherManay1
 
Intake 37 5
Mahmoud Ouf
 
Chapter_4_WP_with_C#_Exception_Handling_student_1.0.pptx
ZachariahAbera
 
12 Exceptions handling
maznabili
 
C# Exceptions Handling
sharqiyem
 
Exceptions
motthu18
 
Intake 38 5
Mahmoud Ouf
 
12. Exception Handling
Intro C# Book
 
Exceptions
DeepikaT13
 
Production Debugging at Code Camp Philly
Brian Lyttle
 
Exception Handlin g C#.pptx
R S Anu Prabha
 
Exception Handling in C#
Abid Kohistani
 
Exception guidelines in c#
Ahmed Helmy
 
C Sharp Tutorial : C Sharp Exception
Courseing Online
 
10 iec t1_s1_oo_ps_session_14
Niit Care
 
Csharp_Chap04
Mohamed Krar
 
Week 2: Getting Your Hands Dirty – Part 2
Jamshid Hashimi
 
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
C sharp syllabus
deepa lakshmi
 
IakakkakjabbhjajjjjjajjajwsjException.pptx
KingDietherManay1
 

Recently uploaded (20)

DOCX
Paper - Suprasegmental Features (Makalah Presentasi)
Sahmiral Amri Rajagukguk
 
PPTX
770043401-q1-Ppt-pe-and-Health-7-week-1-lesson-1.pptx
AizaRazonado
 
PDF
Annual report 2024 - Inria - English version.pdf
Inria
 
PPTX
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
PPTX
Neuroinflammation and microglial subtypes
KanakChaudhary10
 
PPTX
Basal_ganglia_Structure_Function_Importance
muralinath2
 
PPTX
CNS.pptx Central nervous system meninges ventricles of brain it's structure a...
Ashwini I Chuncha
 
PDF
Service innovation with AI: Transformation of value proposition and market se...
Selcen Ozturkcan
 
PDF
Portable Hyperspectral Imaging (pHI) for the enhanced recording of archaeolog...
crabbn
 
PPTX
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
PPTX
LESSON 2 PSYCHOSOCIAL DEVELOPMENT.pptx L
JeanCarolColico1
 
DOCX
Critical Book Review (CBR) - "Hate Speech: Linguistic Perspectives"
Sahmiral Amri Rajagukguk
 
PPTX
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
PDF
A Man of the Forest: The Contributions of Gifford Pinchot
RowanSales
 
PPTX
Q1 - W1 - D2 - Models of matter for science.pptx
RyanCudal3
 
PPTX
Ghent University Global Campus: Overview
Ghent University Global Campus
 
PDF
BlackBody Radiation experiment report.pdf
Ghadeer Shaabna
 
PDF
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
PPT
Experimental Design by Cary Willard v3.ppt
MohammadRezaNirooman1
 
PPTX
ION EXCHANGE CHROMATOGRAPHY NEW PPT (JA).pptx
adhagalejotshna
 
Paper - Suprasegmental Features (Makalah Presentasi)
Sahmiral Amri Rajagukguk
 
770043401-q1-Ppt-pe-and-Health-7-week-1-lesson-1.pptx
AizaRazonado
 
Annual report 2024 - Inria - English version.pdf
Inria
 
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
Neuroinflammation and microglial subtypes
KanakChaudhary10
 
Basal_ganglia_Structure_Function_Importance
muralinath2
 
CNS.pptx Central nervous system meninges ventricles of brain it's structure a...
Ashwini I Chuncha
 
Service innovation with AI: Transformation of value proposition and market se...
Selcen Ozturkcan
 
Portable Hyperspectral Imaging (pHI) for the enhanced recording of archaeolog...
crabbn
 
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
LESSON 2 PSYCHOSOCIAL DEVELOPMENT.pptx L
JeanCarolColico1
 
Critical Book Review (CBR) - "Hate Speech: Linguistic Perspectives"
Sahmiral Amri Rajagukguk
 
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
A Man of the Forest: The Contributions of Gifford Pinchot
RowanSales
 
Q1 - W1 - D2 - Models of matter for science.pptx
RyanCudal3
 
Ghent University Global Campus: Overview
Ghent University Global Campus
 
BlackBody Radiation experiment report.pdf
Ghadeer Shaabna
 
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
Experimental Design by Cary Willard v3.ppt
MohammadRezaNirooman1
 
ION EXCHANGE CHROMATOGRAPHY NEW PPT (JA).pptx
adhagalejotshna
 
Ad

Unit2_2.pptx Chapter 2 Introduction to C#

  • 1. M. SC. (Computer Science) Course Code : 23CsCmpP121 Course Name : DOT NET PresentedBy : Priyanka Jadhav Modern College of Arts,Science and Commerce ,Pune-5
  • 3. i. Chapter 2 Introduction to C# 2.1.3 Functions, Delegates Delegates What is delegate? In C#, delegate is a reference to the method. It works like function pointer in C and C++. But it is objected- oriented, secured and type-safe than function pointer. For static method, delegate encapsulates method only. But for instance method, it encapsulates method and instance both. The best use of delegate is to use as event. Internally a delegate declaration defines a class which is the derived class of System.Delegate.
  • 4. i. Chapter 2 Introduction to C# 2.1.3 Functions, Delegates Delegates A delegate is similar to a class .you can create an instance of it, and when you do so, you pass in the function name as a parameter to the delegate constructor, and it is to this function the delegate will point to . Tip to remember delegate syntax -: delegate syntax look very much similar to a method with a delegate keyword.
  • 5. i. Chapter 2 Introduction to C# 2.1.3 Functions, Delegates Delegates Declaring Delegates  Delegates in c# are declared using the delegate keyword followed by the return type and parameter of the referenced method.  Declaring a delegate is quite similar to declaring a method except that there is no implementation. Thus , the declaration statement must end with a semicolon. public delegate int Calculation(int num1,int num2);
  • 6. i. Chapter 2 Introduction to C# 2.1.3 Functions, Delegates Delegates Note:-if the delegate is declared outside the class, you cannot declare another delegate with the same name in that namespace. Delegates are called as type safe function pointer.
  • 7. i. Chapter 2 Introduction to C# 2.1.3 Functions, Delegates Delegates Types of Delegate:- 1. Single delegate 2. Multicast delegate 3. Generic delegate
  • 8. i. Chapter 2 Introduction to C# 2.1.3 Functions, Delegates Delegates 1. Single delegate:-  Single cast delegate can be used to invoke a single method.  Single cast delegate point to single method at a time.  In this the delegate is assigned to a single method at a time.
  • 9. i. Chapter 2 Introduction to C# 2.1.3 Functions, Delegates Delegates 2. MultiCast Delegate:-  A Multicast delegate is a delegate that has references to more than one function.  When you invoke a multicast delegate, all the functions the delegate is pointing to, are invoked.
  • 10. i. Chapter 2 Introduction to C# 2.1.3 Functions, Delegates There are 2 approaches to create a multicast delegate, depending on the approach you use :-  + or += to register a method with the delegate (adding new method on existing delegate instance)  -or -= to un-register a method with the delegate . ( can be used to remove a method from a delegate instance. ) Note-A multicast delegate, invokes the methods in the invocation list, in the same order in which they are added. Delegates
  • 11. to be cont… Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Error Handling Error handling is the process of responding to and recovering from error conditions in your program. The errors your program will encounter can be classified in two categories:- 1. Syntax Errors 2. Logic Errors
  • 12. to be cont… Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Error Handling Syntax Errors Syntax Errors occur in program due to poor understanding of language syntax or mistake while typing. Few examples of syntax errors in program are missing semicolon at the end of statement, using reserved errors as user defined identifiers ,using variable without declaration etc.
  • 13. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Error Handling Logic Errors Logic Errors occur due to improper implementation of program logic. Compiler does not show any logic error. If there is any logic error in the program then program gives wrong results. Logic errors are difficult to spot.
  • 14. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Debugging Debugging is the routine process of locating and removing computer program bugs, errors or abnormalities, which is methodically handled by software programmers via debugging tools. The Debugging is the program you used to debug your code . The code or application that your are debugging is called debugger.
  • 15. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling Exception Handling is abnormal events that prevent a certain task from being completed successfully. Or in other word - It is process of responding to unwanted or unexpected events when a computer program runs. Exceptions are run-time errors that may cause a program to be immediately terminated. Exception handling is a process of handling these run-time errors. Handling an exception refers to the action to be taken when an error occurs in order to save the program from being prematurely terminated.
  • 16. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling For example:-consider a c# application that is currently being executed. Assume that at some point of time, the common language run-time(CLR) discovers they it does not have the read permission to read a particular file, the CLR immediately stops further processing of the program and terminates its execution abruptly. To avoid this from happening, you can use the exception handling features of c#.
  • 17. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling C# can handle different types of exceptions using exception handling statements. It allows you to handle basically two kinds of exceptions. These are: System Defined Exceptions User Defined Exceptions
  • 18. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling The System.Exception class is the base class that allows you to handle all exception c#. This means that all exception in c# inherit the System.Exception class either directly or indirectly. The system.Exception class contains public and protected method that can be inherited by other exception class. In addition, the System. Exception class contains properties that are common to all exceptions. The table shown alongside describe these properties.
  • 19. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling Properties of Exception Class Properties Descriptions Message Display a message which indicates the reason for the execution. Source Provides the name of the application or the object that caused the exception. StackTrace Provides exception details on the stack at the time the exception was thrown.
  • 20. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling Exceptions Description System.ArithmeticException This expt. Is thrown for problems that occur due to arithmetic or casting and conversion operations. System.ArgumentException This expt. Is thorn when one of the arguments does not match the parameter specifications of the invoked method. System.ArrayTypeMismatchException This exception is thrown when an attempt is made to store data in an array whose type is incompatible with the type of the array.
  • 21. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling Exceptions Description System.DvideByZeroException System.IndexOutOfRangeException System.InvalidCastException System.ArgumentNullExcption This exception is thrown when an attempt is made to divide a numeric value by zero. This exception is thrown when an attempt is made to store data in an array using an index that is less than zero or outside the upper bound of the array. This exception is thrown when an explicit conversion from the base type or interface type to another type fails. This exception is thrown when a null reference is passed to an argument of method that does not accept null values. System.NullReferenceException The exception is thrown when you try to assign a value to a null object. System.OutOfMemoryException This exception is thrown when there is not enough memory to allocate to an object. System.OverflowException This exception. Is thrown when the result of an ariththmetic,casting or conversion operation is too large to be stored in the destination object or variable.
  • 22. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling Exceptions Description System.StackOverflowException This exception is thrown when the stack runs out of space due to having too many pending method calls. System.data.DataException This exception Is thrown when errors are generated while using the ADO.NET components. System.FormatException This exception is thrown when the format of an argument does not match the format of the parameter data type of the invoked method. System.OverflowException This exception. Is thrown when the result of an ariththmetic,casting or conversion operation is too large to be stored in the destination object or variable. Syste.IO.IOEXception This exception is thrown when any I/O error occurs while accessing information using streams , files and directories.
  • 23. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling Catching Exceptions:- Consider a group of boys playing throw ball wherein one boy throws a ball and the other boys catch the ball. If any of the boys fail to catch the ball, the game will be terminated. Thus the game does on till the boys are successful in catching the ball in time. Similarly, in c# exception that occur while working on particular program need to be caught by exception handlers. If the program does not contain the appropriate exception handler , then the program might be terminated.
  • 24. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling Catching Exceptions:- Exception handling is implemented using try-catch construct in c#. This construct consist of two blocks, the try block and the catch block.. The try block encloses statements that might generate exceptions. When these exceptions are thrown, the required actions are perform using the catch block. Thus the catch block consist of the appropriate error-handlers that handle exception. The catch block follows the try block and may or may not contain parameters. If the catch block does not contain any parameter, it can catch any type of exception, if the catch block contains a parameter , it catches the type of exception specified by the parameter.
  • 25. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling The following syntax is used for handling errors using the try and catch blocks. try { //statements that might raise exception or program code } catch(Exceptionclass objException) { //statements for handling the exception } Note:-the catch block is only executed if the try block throws an exception.
  • 26. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling The “throw” Statement:- The throw statement in c# allows you to programmatically throw exceptions using the throw keyword. The throw statement takes an instance of the particular exception class as a parameter. However , if the instance does not refer to the valid exception class, the c# compiler generates an error. When you an exception using the throw keyword, the exception is handled by the catch block.
  • 27. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling The “finally” Statement:- In general, if any of the statements in the try block raises an exception, the catch block is executed and the rest of the statement in the try block are ignored. But , sometimes it becomes mandatory to execute some statements irrespective of whether an exception is raised or not. In such cases, a finally block is used. Examples of action that can be placed in a finally block are: closing a file, assigning object to null, closing a database connection and so forth. The finally block is an optional block appears after the catch block. It enclose statements that are executed regardless of whether an exception occurs.
  • 28. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling Nested “try” blocks The nested try block consists of multiple try-catch constructs. A nested try block starts with a try block, which is called the outer try block. This outer try block contains multiple try blocks within it, which are called inner try blocks. If an exception is thrown by a nested try block, the control passes to its corresponding nested catch block. consider an outer try block containing a nested try-catch –finally construct. If the inner try block throws an exception, control is passed to the inner catch block, however, if the inner catch block does not contain the appropriate error handler, the control is passed to the out catch block.
  • 29. Chapter 2 Introduction to C# 2.1.4 Debugging and error handling, exception handling( System Defined and User Defined) Exception Handling Nested “try” blocks The following syntax is used to create nested try-catch blocks. try { //outer try block try { //inner try block } catch { //inner catch block } //this is optional finally { //inner finally block } } catch { //outer catch block } }