10 iec t1_s1_oo_ps_session_14

30
Slide 1 of 30 Session 14 Ver. 1.0 Object-Oriented Programming Using C# In this session, you will learn to: Handle exceptions Implement the user-defined exceptions Implement threads Define the life cycle of a thread Objectives

Upload: niit-care

Post on 16-Apr-2017

666 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 10 iec t1_s1_oo_ps_session_14

Slide 1 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

In this session, you will learn to:Handle exceptionsImplement the user-defined exceptionsImplement threadsDefine the life cycle of a thread

Objectives

Page 2: 10 iec t1_s1_oo_ps_session_14

Slide 2 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Handling Exceptions

In exception handling, the application is divided into blocks of code. A block that shows the probability of raising an error contains one or more exception handlers. The exception handlers follow a control structure and a uniform way of handling the system level and application level errors.The blocks for exception-handling can be implemented using the following keywords:

trycatchfinally

Let us look at each of these keywords in detail.

Page 3: 10 iec t1_s1_oo_ps_session_14

Slide 3 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Handling Exceptions (Contd.)

The try block:The try block guards statements that may throw an exception. Following is the syntax of try block statement:try{//statements that may cause an exception}

The try block governs statements that are enclosed within it and defines the scope of the exception-handlers associated with it.A try block must have at least one catch block.

Page 4: 10 iec t1_s1_oo_ps_session_14

Slide 4 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Handling Exceptions (Contd.)

The catch block:The catch statement of the catch block takes an object of the exception class as a parameter, which refers to the raised exception. You can associate an exception-handler with the try block by providing one or more catch handlers, immediately after the try block:try{ //statements that may cause an exception}catch (…){//error handling code

}

Page 5: 10 iec t1_s1_oo_ps_session_14

Slide 5 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Handling Exceptions (Contd.)

The finally block:The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown:try{ //statements that may cause an exception}catch (…){//error handling code

}finally{//statements to be executed

}

Page 6: 10 iec t1_s1_oo_ps_session_14

Slide 6 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

Problem Statement:David is working on a project where he is calculating the sum of values in an integer array. David needs to handle the exceptions, which can occur while he is working with the arrays. If any exceptional condition is reached when David is executing the application, the application needs to display an exception message.Help David to handle the exceptions.

Demo: Handling Exception for Arrays Beyond Limit

Page 7: 10 iec t1_s1_oo_ps_session_14

Slide 7 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

Solution:To develop a console-based application, David need to perform the following tasks:

1. Create a console-based application.2. Build and execute an application.

Demo: Handling Exception for Arrays Beyond Limit (Contd.)

Page 8: 10 iec t1_s1_oo_ps_session_14

Slide 8 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Implementing the User-Defined Exceptions

In C#, you can create your own exception class. Such kinds of exceptions are known as user-defined exceptions.The Exception class is the base class for all the exceptions in C#. The user-defined exception classes must follow the hierarchy of either the exception class or of one of the standard inherited classes.

Page 9: 10 iec t1_s1_oo_ps_session_14

Slide 9 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Implementing the User-Defined Exceptions (Contd.)

User-defined exception classes are derived from the ApplicationException class. To implement user-defined exceptions, you need to:

Raise your exception: You can use the throw statement to raise your own exceptions. Throw an object: You can throw an object if the object is either directly or indirectly derived from System.Exception. You can use a throw statement in the catch block to throw the present object, as shown in the following code:catch(Exception caught){. . .

throw caught}

Page 10: 10 iec t1_s1_oo_ps_session_14

Slide 10 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

A thread is defined as the execution path of a program.You can define a unique flow of a control in a program, using a thread.Threads are used to run applications that perform large and complex computations.A process that is executed using one thread is known as a single-threaded process, where the process is a running instance of a program.Single-threaded application can perform only one task at a time. You have to wait for one task to complete before another task can start.

Implementing Threads

Page 11: 10 iec t1_s1_oo_ps_session_14

Slide 11 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

The following figure shows a single-threaded process.

To execute more than one task at a time, you can create multiple threads in a program. A process that creates two or more threads is called a multithreaded process.

Implementing Threads (Contd.)

Page 12: 10 iec t1_s1_oo_ps_session_14

Slide 12 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Implementing Threads (Contd.)

The following figure shows a multithreaded process.

Page 13: 10 iec t1_s1_oo_ps_session_14

Slide 13 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#The Thread Model in C#

In single-threaded systems, an approach called event loop with polling is used. Polling is the process in which a single event is executed at a time. In the event loop with polling approach, a single thread runs in an infinite loop till its operation is completed.In a single-threaded application if the thread is suspended from execution because it is waiting for a system resource, the entire program stops executing.In multithreading, the time for which a thread waits for the CPU time can be utilized to perform another task.

Page 14: 10 iec t1_s1_oo_ps_session_14

Slide 14 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#The Thread Model in C#(Contd.)

In C#, you will use the Thread class to work with threads.The System.Threading.Thread class is used to construct and access individual threads in a multithreaded application.

Page 15: 10 iec t1_s1_oo_ps_session_14

Slide 15 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#The Main Thread

The main thread is created automatically on the start up of a C# program execution. The threads which are created exclusively using the Thread class are called as child threads, where the main thread is called a parent thread or a primary thread.You can access a thread using the CurrentThread property of the Thread class.

Page 16: 10 iec t1_s1_oo_ps_session_14

Slide 16 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

In C#, you create a thread by creating an object of type Thread, giving its constructor a ThreadStart reference, and calling the new thread’s Start() method.The new thread starts executing asynchronously with an invocation of the thread’s method.There are various methods available with the Thread class. Using these methods, you can control the execution of threads. Few of these methods are:

Start(): Starts a thread.Sleep(): Makes the thread to pause for a period of time.Abort(): Terminates the thread.Suspend(): Suspends a thread. If the thread is already suspended it has no effect.Resume(): Resumes the suspended thread.

Working with Threads

Page 17: 10 iec t1_s1_oo_ps_session_14

Slide 17 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

You can create threads by extending the Thread class. The extended thread class calls the Start() method to begin the child thread execution. Following is an example of creating threads: ThreadStart ChildRef = newThreadStart(ChildThreadCall);Thread ChildThread = new Thread(ChildRef);ChildThread.Start();

Creating Threads

Page 18: 10 iec t1_s1_oo_ps_session_14

Slide 18 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Managing Threads

There are many tasks you might need to perform to manage the activity or life of a thread.You can manage all these tasks by using the various thread methods available with the Thread class.The static Thread.Sleep() method calls the static CurrentThread method, which then pauses that thread for the specified amount of time.

Page 19: 10 iec t1_s1_oo_ps_session_14

Slide 19 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Destroying Threads

If the thread is required to be destroyed, the Thread.Abort() method will allow you to accomplish the task. The runtime aborts the thread by throwing a ThreadAbortException. This exception cannot be caught. If the finally block is present in the method, the runtime will send the control to it.

Page 20: 10 iec t1_s1_oo_ps_session_14

Slide 20 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#Thread Life Cycle

The lifecycle of a thread starts when an object of the System.Threading.Thread class is created. The life cycle of the thread ends with task execution.There are various states in the life cycle of a thread. These states are:

The Unstarted stateThe Runnable stateThe Not Runnable stateThe Dead state

Let us understand the life cycle of the thread with the help of the following figure.

Page 21: 10 iec t1_s1_oo_ps_session_14

Slide 21 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

Start() Started

Work Completed Stopped

Page 22: 10 iec t1_s1_oo_ps_session_14

Slide 22 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

Start() Started Suspend() Suspended

Resume()

Page 23: 10 iec t1_s1_oo_ps_session_14

Slide 23 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

Start() Started

Sleep() Wait/JoinSleep

Interrupt()

Time Expires

Page 24: 10 iec t1_s1_oo_ps_session_14

Slide 24 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

Start() Started

Abort() Stop RequestThread Responds to Stop Request

Stopped

Page 25: 10 iec t1_s1_oo_ps_session_14

Slide 25 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

When an instance of the Thread class is created, the thread enters the unstarted state.A new thread is an empty object of the Thread class, and no system resources such as memory are allocated to it.

The Unstarted State

Page 26: 10 iec t1_s1_oo_ps_session_14

Slide 26 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

The thread remains in the unstarted state until the program calls the Start() method of the Thread class, which places the thread in the runnable state and immediately returns control to the calling thread.This state is also called as the ready or started state. The newly started thread and any other threads in the program execute concurrently.

The Runnable State

Page 27: 10 iec t1_s1_oo_ps_session_14

Slide 27 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

A thread is not in the runnable state if it is:SleepingWaitingBlocked

The Not Runnable State

Page 28: 10 iec t1_s1_oo_ps_session_14

Slide 28 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#The Dead State

A running thread enters the dead state when the statements of the threads method are complete. This state is also called the terminated state. A program can force a thread into the dead state by calling the Abort() method of the Thread class on the appropriate thread object.

Page 29: 10 iec t1_s1_oo_ps_session_14

Slide 29 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

In this session, you learned that:Exception handling is implemented using the following keywords:

trycatchfinally

Exception-handling provides a structured and uniform way of handling system-level and application-level errors.Exception handling is the process of providing an alternative path to be executed when an application is not able to execute in the desired.In addition to handling pre-defined exceptions, users can create their own exceptions by deriving an exception class from the ApplicationException class.

Summary

Page 30: 10 iec t1_s1_oo_ps_session_14

Slide 30 of 30Session 14Ver. 1.0

Object-Oriented Programming Using C#

You can only throw an object if the types of objects either directly or indirectly derives from System.Exception.You can use the throw statement to raise your own exceptions.A thread is defined as the path of execution of a program. It is a sequence of instructions that is executed to define a unique flow of control.A program that creates two or more threads is called a multithreaded program.The System.Threading class is used to construct and access individual threads in a multithreaded application.The various states in the life cycle of a thread are:

Unstarted stateRunnable stateNot Runnable stateDead state

Summary (Contd.)