0
Chapter 6: Exception Handling
1 /59
Chapter 4: OOP with C# 2.0
2 /59
Reviewing OOP Concepts.
OOP - Encapsulation.
OOP - Inheritance.
OOP - Polymorphism.
C# Casting Rules
Partial Types
3 /59
Procedure-Oriented Program
Class A
{
data1
data1
data2 Modifiers Function1 ()
Function1 (data1)
Function2 ()
Function2 (data1) }
Function3 (data2)
Function4 (data2)
Class B
{
data2
Object = Data + Methods
Modifiers Function3 ()
4 /59
Aggregation of data and behavior:
• Data of a class should be hidden from the outside.
5 /59
Using
accessor traditional
accessor and
mutator
Mutator
6 /59
Using
Properties
7 /59
Ability allows a class having members of an existed class
Re-used code.
class PERSON class STUDENT
ID_Num ID_Num
Name Name
YearOfBirth inherited YearOfBirth
Address Address
getID_Num() getID_Num()
setID_Num(newID) setID_Num(newID)
...... ......
“is a”
relationship
RollNum
class STUDENT Score
extensions
getScore()
RollNum setSore(newScore)
Score ......
getScore()
setSore(newScore)
...... Son = Father + extensions
8 /59
Inheritance Implementation in C#
Invoke parent’s
constructor
Invoke parent’s
constructor
9 /59
Ability allows many versions of a method based on overloading
and overriding methods techniques.
10 /59
Polymorphism Implementation in C#
virtual : provide a default implementation. Can
be overridden if necessary
Optional
Must
??
Besides virtual and abstract, C# provide new
keyword that applies to methods. What does
that mean? 11 /59
Value type: conversion and casting
Lost data
13 /59
Same class name
14 /59
15
Chapter 6: Exception Handling
16 /59
Errors,Bugs, Exceptions
Catching Exception: try – catch – finally
Exception Propagation
17 /59
Bugs: an error on the part of the programmer.
• For example, making calls on a NULL pointer
18 /59
When an exception occurs, the program will terminate
abruptly
Example Exception
message
Exception
details
19 /59
try
try block {
< statements may cause exceptions >
}
false
e? catch ( ExceptionType1 e1 )
{
true
< statements handle the situation 1>
catch block }
catch ( ExceptionType2 e2)
{
< statements handle the situation 2>
finally block }
finally
{
< statements are always executed >
}
20 /59
Exception handling example
21 /59
catch(...)
Stack for A() A()
Stack for B()
B()
Stack for C()
Stack for D() C()
22 /59
Exception handling : stacktrace example
23 /59
24
Thank you
25