cs313d: advanced programming language · chapter 13 : 13.1 13.3 exception handling dr. amal...

49
CS313D: ADVANCED PROGRAMMING LANGUAGE Lecture 6 : Abstraction Computer Science department

Upload: others

Post on 19-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

CS313D: ADVANCED

PROGRAMMING LANGUAGE

Lecture 6 : Abstraction Computer Science

department

Page 2: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Lecture Contents

dr. Amal Khalifa, Spr 17

Abstract classes

Abstract methods

Case study: Polymorphic processing

Sealed methods & classes

Interfaces

Exception handling

2

Page 3: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Chapter 12 : 12.4, 12.5, 12.6

Abstraction 3

dr. Amal Khalifa, Spr 17

Page 4: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Abstract Classes and Methods

Abstract classes, or abstract base classes cannot

be used to instantiate objects.

too general, specify only what is common among

derived classes.

contains one or more abstract methods

keyword abstract in their declaration

do not provide implementations.

Constructors and static methods cannot be declared abstract.

Classes that can be used to instantiate objects are

called concrete classes.

provide implementations to the abstract methods. dr. Amal Khalifa, Spr 17

4

Page 5: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Abstract Properties

public abstract PropertyType MyProperty

{ get; set;

} // end abstract property

An abstract property omits implementations for the get

accessor and/or the set accessor.

Concrete derived classes must provide implementations for

every accessor declared in the abstract property.

dr. Amal Khalifa, Spr 17

5

Page 6: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Tips !!

dr. Amal Khalifa, Spr 17

6

Page 7: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Case Study: Payroll System

A company pays its employees on a weekly basis. The employees

are of four types: Salaried employees are paid a fixed weekly

salary regardless of the number of hours worked, hourly

employees are paid by the hour and receive "time-and-a-half"

overtime pay for all hours worked in excess of 40 hours,

commission employees are paid a percentage of their sales, and

salaried-commission employees receive a base salary plus a

percentage of their sales. For the current pay period, the

company has decided to reward salaried-commission employees

by adding 10% to their base salaries. The company wants to

implement an app that performs its payroll calculations

polymorphically.

dr. Amal Khalifa, Spr 17

7

Page 8: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Design

dr. Amal Khalifa, Spr 17

8

Page 9: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 9

Page 10: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 10

Page 11: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 11

Page 12: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 12

Page 13: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 13

Page 14: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 14

Page 15: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 15

Page 16: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 16

Page 17: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 17

Page 18: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 18

Page 19: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 19

Page 20: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 20

Page 21: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 21

Page 22: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 22

Page 23: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 23

Page 24: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

sealed Methods and Classes

dr. Amal Khalifa, Spr 17

A method declared sealed in a base class cannot be

overridden in a derived class.

Methods that are declared private are implicitly sealed.

Methods that are declared static also are implicitly sealed,

because static methods cannot be overridden either.

A derived-class method declared both override and

sealed can override a base-class method, but cannot be

overridden in classes further down the inheritance hierarchy.

Calls to sealed methods (and non-virtual methods) are

resolved at compile time—this is known as static binding.

24

Page 25: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

sealed Methods and Classes

dr. Amal Khalifa, Spr 17

A class that is declared sealed cannot be a base

class (i.e., a class cannot extend a sealed class).

All methods in a sealed class are implicitly sealed.

Class string is a sealed class. This class cannot

be extended, so apps that use strings can rely

on the functionality of string objects as specified

in the Framework Class Library.

25

Page 26: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Interfaces 26

dr. Amal Khalifa, Spr 17

Page 27: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Interfaces

An interface declaration begins with the keyword

interface and can contain only abstract methods and

abstract properties

All interface members are implicitly declared both public

and abstract.

An interface can extend one or more other interfaces to create

a more elaborate interface that other classes can implement.

specify that it implements the interface

must declare each member of the interface with the signature

specified in the interface declaration.

If any interface member is not implemented declare as

abstract dr. Amal Khalifa, Spr 17

27

Page 28: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Case study

dr. Amal Khalifa, Spr 17

28

Page 29: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 29

Page 30: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 30

Page 31: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 31

Page 32: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 32

Page 33: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 33

Page 34: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 34

Page 35: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Chapter 12 : 12.4, 12.5, 12.6, 12.7

Text Book

dr. Amal Khalifa, Spr 17

35

Page 36: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Chapter 13 : 13.1 13.3

Exception Handling

dr. Amal Khalifa, Spr 17

36

Page 37: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Introduction

An exception is an indication of a problem that

occurred during a program’s execution.

Exception handling enables you to create apps that

can handle exceptions—in many cases allowing a

program to continue executing as if no problems were

encountered.

Exception handling enables you to write clear, robust

and more fault-tolerant programs.

dr. Amal Khalifa, Spr 17

37

Page 38: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Example

dr. Amal Khalifa, Spr 17

an exception

is thrown (i.e.,

an exception

occurs) when a

method

detects a

problem.

38

Page 39: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 39

Page 40: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Example revisited

dr. Amal Khalifa, Spr 17

40

catch and

handle

exceptions —

displaying an

error message

and allowing

the user to

enter another

set of values.

You may also

use

Int32.TryParse

Page 41: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Example revisited

dr. Amal Khalifa, Spr 17

41

Page 42: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 42

Page 43: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

•At least one catch block must immediately follow a try block.

•The statements in the finally clause are guaranteed to execute

regardless of whether an exception occurs.

Exception Handling mechanism 43

dr. Amal Khalifa, Spr 17

Page 44: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

try-catch

dr. Amal Khalifa, Spr 17

44

The point at which an exception occurs is called the throw

point.

If an exception occurs in a try block, program control

immediately transfers to the first catch block matching

the type of the thrown exception.

After the exception is handled, program control resumes

after the last catch block.

An uncaught exception (or unhandled exception) is an

exception for which there is no matching catch block.

Page 45: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

When an exception is thrown, CLR begins searching an exception handler in the call-stack starting from the method that has thrown the exception. This is repeated for each of the methods down the call-stack until a handler is found which catches the exception. If Main(…) is reached and no handler is found, CLR catches the exception and usually displays an error message

method call and exception handling process 45

dr. Amal Khalifa, Spr 17

Page 46: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

.NET Exception Hierarchy

dr. Amal Khalifa, Spr 17

46

In C#, only objects of class Exception and its

derived classes may be thrown and caught.

If a program attempts to access an out-of-range array

index CLR throws IndexOutOfRangeException.

Attempting to use a null reference causes a

NullReferenceException.

A catch block that specifies a parameter of type

Exception can catch all exceptions.

makes sense only if the handling behavior is the same for a

base class and all derived classes.

Page 47: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Be careful !! 47

dr. Amal Khalifa, Spr 17

Page 48: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

Case Study 48

dr. Amal Khalifa, Spr 17

Page 49: CS313D: ADVANCED PROGRAMMING LANGUAGE · Chapter 13 : 13.1 13.3 Exception Handling dr. Amal Khalifa, Spr 17 36 . Introduction An exception is an indication of a problem that occurred

dr. Amal Khalifa, Spr 17 49