[Link].
(Computer Science)
SEM-II
Course Code : 19ScCmpP206
Course Name : DOT NET
Presented By: Smita Achalkar
Modern College of Arts,Science and
Commerce ,Pune-5
Notes
Chapter 2
Introduction to C#
Chapter 2
Introduction to C#
Last Lecture We had learned the following points which are
mentioned:
Chapter 2
Introduction to C#
Points to be covered in today’s Lecture
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. 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.
to be cont…
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 are abnormal events that prevent a certain
task from being completed successfully.
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
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 [Link] class is the base class that allows you to
handle all exception c#. This means that all exception in c# inherit
the [Link] class either directly or indirectly.
The [Link] 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
Properties of Exception Class 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
The [Link] class has a number of derived
exception classes to handle different types of exceptions.
The hierarchy shown below displays the different
exception classes in the System namespace.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
The system namespaces contains the different exception
classes that c# provides. The type of exception to be handled
depends on the specified exception class. The table shown
below lists some of the commonly used exception classes.
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
Exception Handling
Exceptions Description
[Link] This expt. Is thrown for problems that occur
due to arithmetic or casting and conversion
operations.
[Link] This expt. Is thorn when one of the
arguments does not match the parameter
specifications of the invoked method.
[Link] 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
Exceptions Description
Introduction to C#
[Link] This exception is thrown when an attempt is
made to divide a numeric value by zero.
This exception is thrown when an attempt is
2.1.4 Debugging and error handling,
[Link] exception
made to store data in an array using an
index that is less than zero or outside the
handling( System Defined and ofUser
upper bound Defined)
the array.
Exception Handling
[Link] 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
[Link] reference is passed to an argument of
method that does not accept null values.
[Link] The exception is thrown when you try to
assign a value to a null object.
[Link] This exception is thrown when there is not
enough memory to allocate to an object.
[Link] 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
Exceptions Description
Introduction to C#
[Link] This exception is thrown when the stack
runs out of space due to having too many
2.1.4 Debugging and error pending
handling,methodexception
calls.
handling( System Defined
[Link] and IsUser
This exception thrownDefined)
when errors are
generated while using the [Link]
components.
Exception Handling
[Link] This exception is thrown when the format of
an argument does not match the format of
the parameter data type of the invoked
method.
[Link] 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.
[Link] 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 //this is optional
{ finally
//outer try block {
try //inner finally block
{ }
//inner try block }
} catch
catch {
{ //outer catch block
//inner catch block }
} }
Chapter 2
Introduction to C#
2.1.4 Debugging and error handling, exception
handling( System Defined and User Defined)
User Defined Exception Handling
Custom exception are user-defined exceptions that allow
users to recognize the cause of unexpected events in specific
programs and display custom message. Thus, custom
exception allow you to simplify and improve the process of
error-handling in a program. Even though c# provides you
with a rich set of exception classes, they do not address
certain application—specific and system-specific
constraints. To handle such constraints, you can define
custom exceptions.
Custom exception can be created to handle exceptions that
are thrown by the CLR as well as those that are thrown by
user applications.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Defining class
Object of class
Access modifier
Feature of OOPs
Inheritance
Abstraction
Polymorphism
Encapsulation
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Access modifier
Access modifiers are keywords used to specify the
declared accessibility of a member or a type.
Access modifiers are an integral part of object-
oriented programming. They support the concept
of encapsulation, which promotes the idea of
hiding functionality. Access modifiers allow you to
define who does or doesn't have access to certain
features.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Access modifier
It’s special kind of modifiers using which we can
define the scope of a type(class) and it’s members.
1. Private
2. Internal
3. Protected
4. Protected internal
5. Public
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Access modifier
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Inheritance is one of the three foundational principles of
Object-Oriented Programming (OOP) because it allows
the creation of hierarchical classifications. Using
inheritance you can create a general class that defines
traits common to a set of related items. This class can
then be inherited by other, more specific classes, each
adding those things that are unique to it.
In the language of C#, a class that is inherited is called a
base class. The class that does the inheriting is called the
derived class. Therefore a derived class is a specialized
version of a base class.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Inheritance is one of the three foundational principles of
Object-Oriented Programming (OOP) because it allows
the creation of hierarchical classifications. Using
inheritance you can create a general class that defines
traits common to a set of related items. This class can
then be inherited by other, more specific classes, each
adding those things that are unique to it.
In the language of C#, a class that is inherited is called a
base class. The class that does the inheriting is called the
derived class. Therefore a derived class is a specialized
version of a base class.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Multiple inheritance using Interfaces
E example
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
An abstract class is an incomplete class or special class we
can't be instantiated. The purpose of an abstract class is to
provide a blueprint for derived classes and set some rules what
the derived classes must implement when they inherit an
E abstract class.
We can use an abstract class as a base class and all derived
classes must implement abstract definitions. An abstract method
must be implemented in all non-abstract classes using the
override keyword. After overriding the abstract method is in the
non-Abstract class. We can derive this class in another class and
again we can override the same abstract method with it.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Constructors
E
• It ‘s special method present under a class responsible for
initializing the variables of that class.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Constructors
The name of constructor method is exactly the same
name of the class in which it was present and more over
it’s a non-value returning method.
E
Each and every class require this constructor ., if we
want to create the instance of that class.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Constructors
• Example:-
• Class Test
E •{
• int i;
•}
• Test obj =new Test();
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Constructors
it’s is the responsibility of a programmer to define a
constructor under his class and if he fails to do so , on
E behalf of the programmer an implicit constructor gets
defined in that class by the compiler.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Constructors
Implicitly defined constructors are parameter less
constructor and these constructor s are also known as
default constructor.
E
Implicitly defined constructor are public .
We can also defined a constructor under the class and if
we defined it we can call it as explicit constructor and
explicit constructor can be parameter less or
parameterized constructor also .
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Types of Constructors
Default or Parameter less Constructor
Parameterized Constructor
E Copy Constructor
Static Constructor
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Default or Parameter less Constructor
if a Constructor method doesn’t take parameter
E • an parameters then we call that as default or parameter
less Constructor.
These Constructor can be defined by a programmer
explicitly or else will be default Implicitly provided there is
no explicit constructor under the class.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Parameterized Constructor
E
if a constructor method is defined with any parameters w
call that as parameterized constructor and these
constructor can be defined by programmers only that
never can defined implicitly.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Static Constructor
if a constructor is explicitly declared by using static
modifiers we call that as Static Constructor . All the
constructors we have defined till now are non-static or
instance constructors.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Static Constructor
If a class contains any static variables then only
implicit static constructor will be present or else
we need to define them explicitly whereas non-
static constructors will be implicitly defined in
every class(except static class) provided we did
not define them explicitly.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Static Constructor
Static constructor are responsible in initializing
static variables and these constructors are never
called explicitly they are implicitly called and more
over these constructors are first to execute under
any class.
Static constructor can’t be parameterized so
overloading static constructor is not possible.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Function overloading
Function overriding(Virtual function)
Hiding methods