06cs761 Ppts Chapter IV

80
EXCEPTIONS AND OBJECT LIFE TIME Mr. G.C.Deshpande Lecturer, [email protected]

description

C# Programming and .Net Unit 4

Transcript of 06cs761 Ppts Chapter IV

Page 1: 06cs761 Ppts Chapter IV

EXCEPTIONS AND OBJECT LIFE TIMEMr. G.C.Deshpande

Lecturer, [email protected]

Page 2: 06cs761 Ppts Chapter IV

Topic Level Objectives To confabulate about handling runtime anomalies in

the code base through the use of structured exception handling.

To examine the memory management process using the .NET garbage collector. To familiarize the C# keywords such as try, catch, throw and finally.

To examine the distinction between application-level and system-level exceptions.

To inspect various tools within Visual Studio 2012 to debug the exceptions.

To elucidate the role of application roots, object generations, and the System.GC type.

To confabulate disposable objects (via the IDisposable interface) and finalization process (via the System.Object.Finalize() method).

Page 3: 06cs761 Ppts Chapter IV

Intended Learning Outcomes1. Investigate various tools within Visual Studio 2012 to debug the exceptions [L 4].

2. Manage the memory using .NET garbage collector [L 3].

3. Clarify the distinction between classes, objects and references [L 2].

4. Explain the role of application roots [L 2].

5. Determine the objects reachable by application roots [L 2].

6. Describe System.GC members and their meaning in life [L 1].

7. Illustrate select members of System.GC type [L 5].

8. Explain the finalization process [L 2].

9. Implement finalizable and disposable objects [L 3].

10. Explain a formalized diaposal pattern [L 2].

11. Define anomaly-centric terms [L 1].

12. Explain the role of .NET exception handling and its atoms [L 2].

13. Explain various properties in configuring the state of an exception [L 2].

14. Differentiate between system level exceptions and application level exceptions [L 4].

15. Build custom/application level exceptions [L 5].

16. Explain the processing of multiple exceptions and the finally block [L 2].

17. Demonstrate the debugging of unhandled exceptions using visual studio 2012 [L 3].

Page 4: 06cs761 Ppts Chapter IV

Structured Exception Handling [SEH] Bugs: Error made by the programmer

User Errors: Error made by the user

Exceptions: Really exceptional circumstances !!!.. Runtime anomalies that are difficult to account for while programming your application.

SEH : Dealing with runtime anomalies.

Page 5: 06cs761 Ppts Chapter IV

Role of .NET Exception Handling

Page 6: 06cs761 Ppts Chapter IV

Structured Exception Handling [SEH] Lot of Numerical codes

Lack of symmetry

SEH : Unified approach to exceptional handling common to all languages

Human readable description of errors

Page 7: 06cs761 Ppts Chapter IV

The Atoms of .NET Exception Handling

Page 8: 06cs761 Ppts Chapter IV

The System.Exception Base Class

Page 9: 06cs761 Ppts Chapter IV

The System.Exception Base Class

Page 10: 06cs761 Ppts Chapter IV

Core members of System.Exception type

Page 11: 06cs761 Ppts Chapter IV

Core members of System.Exception type

Page 12: 06cs761 Ppts Chapter IV

The simplest possible example

Page 13: 06cs761 Ppts Chapter IV

The simplest possible example

Page 14: 06cs761 Ppts Chapter IV

The simplest possible example

Page 15: 06cs761 Ppts Chapter IV

The simplest possible example

Page 16: 06cs761 Ppts Chapter IV

The simplest possible example

Page 17: 06cs761 Ppts Chapter IV

Throwing a General Exception

Page 18: 06cs761 Ppts Chapter IV

Catching Exceptions

Page 19: 06cs761 Ppts Chapter IV

Catching Exceptions

Page 20: 06cs761 Ppts Chapter IV

Catching Exceptions

Page 21: 06cs761 Ppts Chapter IV

Defining the Pillars of OOPEncapsulation: How does this language hide an object’s internal implementation details and preserve data integrity? Inheritance: How does this language promote code reuse? Polymorphism: How does this language let you treat related objects in a similar way?

Page 22: 06cs761 Ppts Chapter IV

Role of Encapsulation

Page 23: 06cs761 Ppts Chapter IV

Role of Inheritance [ is-a ]

Page 24: 06cs761 Ppts Chapter IV

Role of Inheritance [ has-a ]

Page 25: 06cs761 Ppts Chapter IV

Role of Polymorphism

Page 26: 06cs761 Ppts Chapter IV

Role of Polymorphism

Page 27: 06cs761 Ppts Chapter IV

C# Access Modifiers

Page 28: 06cs761 Ppts Chapter IV

C# Access Modifiers

Page 29: 06cs761 Ppts Chapter IV

The first pillar: Encapsulation

Page 30: 06cs761 Ppts Chapter IV

The first pillar: Encapsulation

Object’s internal data should not be directly accessible from an object instanceAlter the state of an object indirectly using

Accessors (get) and Mutators (set) .Net property

Black Box Programming

Page 31: 06cs761 Ppts Chapter IV

Traditional Encapsulation

Page 32: 06cs761 Ppts Chapter IV

Traditional Encapsulation

Page 33: 06cs761 Ppts Chapter IV

Encapsulation using Accessors and Mutators

Page 34: 06cs761 Ppts Chapter IV

Encapsulation using Accessors and Mutators

Page 35: 06cs761 Ppts Chapter IV

Encapsulation using .Net Properties

Page 36: 06cs761 Ppts Chapter IV

Encapsulation using .Net Properties

Page 37: 06cs761 Ppts Chapter IV

Encapsulation using .Net Properties

Page 38: 06cs761 Ppts Chapter IV

Encapsulation using .Net Properties

Page 39: 06cs761 Ppts Chapter IV

Controlling Visibility Levels of Properties

read-only : omit the set blockwrite-only : omit the get block

Page 40: 06cs761 Ppts Chapter IV

Chaining of Constructors

Page 41: 06cs761 Ppts Chapter IV

Chaining of Constructors

Page 42: 06cs761 Ppts Chapter IV

Basic Mechanics of Inheritance

Page 43: 06cs761 Ppts Chapter IV

Basic Mechanics of Inheritance

Page 44: 06cs761 Ppts Chapter IV

Basic Mechanics of Inheritance

Page 45: 06cs761 Ppts Chapter IV

The sealed keyword

Page 46: 06cs761 Ppts Chapter IV

Inheritance

Page 47: 06cs761 Ppts Chapter IV

Inheritance

Page 48: 06cs761 Ppts Chapter IV

Controlling the base class creation

Page 49: 06cs761 Ppts Chapter IV

Controlling the base class creation

Page 50: 06cs761 Ppts Chapter IV

Controlling the base class creation Some properties may be readonly

Inefficient way of creating a constructor: since base class

Default constructor is called before the logic of derived class constructor

Page 51: 06cs761 Ppts Chapter IV

Controlling the base class creation

Page 52: 06cs761 Ppts Chapter IV

Arrays in C# An array is a set of data items, accessed using a numerical index

An array is a set of contiguous data points of the same type

Page 53: 06cs761 Ppts Chapter IV

Arrays in C#

Page 54: 06cs761 Ppts Chapter IV

C# Array Initialization Syntax

Page 55: 06cs761 Ppts Chapter IV

Implicitly typed local arrays

Page 56: 06cs761 Ppts Chapter IV

Array of objects

Page 57: 06cs761 Ppts Chapter IV

Multidimensional Arrays [ Rectangular ]Declarationint[,] myArray = new int[4,2];

int[,,] myArray = new int [4,2,3];

Initializationint[,] myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}};int[,] myArray = {{1,2}, {3,4}, {5,6}, {7,8}};

int[,] myArray;myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}}; // OKmyArray = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error

Page 58: 06cs761 Ppts Chapter IV

Multidimensional Arrays [ Rectangular ]

Page 59: 06cs761 Ppts Chapter IV

Multidimensional Arrays [ Jagged ]

A jagged array is an array whose elements are arrays.

The elements of a jagged array can be of different dimensions and sizes.

A jagged array is sometimes called an "array-of-arrays.“ 

Declaration

int[][] myJaggedArray = new int[3][];

Initialization

myJaggedArray[0] = new int[5];

myJaggedArray[1] = new int[4];

myJaggedArray[2] = new int[2];

myJaggedArray[0] = new int[] {1,3,5,7,9}; myJaggedArray[1] = new int[] {0,2,4,6}; myJaggedArray[2] = new int[] {11,22};

Page 60: 06cs761 Ppts Chapter IV

Multidimensional Arrays [ Jagged ]Initialization

int[][] myJaggedArray = new int [][] { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22}

};

int[][] myJaggedArray = { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22}

};

Page 61: 06cs761 Ppts Chapter IV

Multidimensional Arrays [ Jagged ]Initialization

// Assign 33 to the second element of the first array: myJaggedArray[0][1] = 33;

// Assign 44 to the second element of the third array: myJaggedArray[2][1] = 44;

Mixing of arraysint[][,] myJaggedArray = new int [3][,] {

new int[,] { {1,3}, {5,7} },new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {11,22}, {99,88}, {0,9} }

};

Page 62: 06cs761 Ppts Chapter IV

Multidimensional Arrays [ Jagged ]

Page 63: 06cs761 Ppts Chapter IV

The System.Array Base Class

Page 64: 06cs761 Ppts Chapter IV

Methods and Parameter Modifiers

Page 65: 06cs761 Ppts Chapter IV

Methods and Parameter Modifiers

Page 66: 06cs761 Ppts Chapter IV

Default Parameter Passing Behavior

Page 67: 06cs761 Ppts Chapter IV

The out Modifier

Page 68: 06cs761 Ppts Chapter IV

The out Modifier

Page 69: 06cs761 Ppts Chapter IV

The ref Modifier

Page 70: 06cs761 Ppts Chapter IV

The params Modifier

Page 71: 06cs761 Ppts Chapter IV

The params Modifier

Page 72: 06cs761 Ppts Chapter IV

Defining Optional Parameters

Page 73: 06cs761 Ppts Chapter IV

Defining Optional Parameters

Page 74: 06cs761 Ppts Chapter IV

Value Types and Reference Types

Page 75: 06cs761 Ppts Chapter IV

Value Types and Reference Types

Page 76: 06cs761 Ppts Chapter IV

BoxingConversion from value type to reference type

int m=100;

object om=m; //creates a box to hold m

int m=100;

object om=(object)m; //c-style casting

int m=10;

object om=m;

m=20;

console.writeline(m); //m=20

console.writeline(om); //om=10

Page 77: 06cs761 Ppts Chapter IV

UnboxingConversion from reference type to value type

int m=10;

object om=m; //box m

int n=(int)om; //unbox om

int m=500;

object om=m

byte n=(byte)om; // run-time error

Remember: We can only unbox a variable that has previously been boxed

Page 78: 06cs761 Ppts Chapter IV

References

1] Andrew Troelsen, Pro C# with .NET 3.0, Special Edition,

Dream tech Press, India, 2007.

2] E. Balagurusamy, Programming in C#, 5th Reprint, Tata McGraw Hill,

2004 (For Programming Examples).

3] Tom Archer, Inside C#, WP Publishers, 2001.

4] Herbert Schildt, C#: The Complete Reference, TMH, 2004. 

5] Yashavant Kanetkar, C#.NET fundas, First Edition, BPB Publications,

2002

 

Page 79: 06cs761 Ppts Chapter IV

Contact Me

Email: [email protected]

[email protected]

Blog: gcdeshpande.spaces.live.com

Follow on twitter: www.twitter.com/gcdeshpande

Page 80: 06cs761 Ppts Chapter IV