The Codex of Business Writing Software for Real-World Solutions 2.pptx
10 iec t1_s1_oo_ps_session_14
1. Object-Oriented Programming Using C#
Objectives
In this session, you will learn to:
Handle exceptions
Implement the user-defined exceptions
Implement threads
Define the life cycle of a thread
Ver. 1.0 Session 14 Slide 1 of 30
2. 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:
try
catch
finally
Let us look at each of these keywords in detail.
Ver. 1.0 Session 14 Slide 2 of 30
3. 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.
Ver. 1.0 Session 14 Slide 3 of 30
4. 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
}
Ver. 1.0 Session 14 Slide 4 of 30
5. 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
}
Ver. 1.0 Session 14 Slide 5 of 30
6. Object-Oriented Programming Using C#
Demo: Handling Exception for Arrays Beyond Limit
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.
Ver. 1.0 Session 14 Slide 6 of 30
7. Object-Oriented Programming Using C#
Demo: Handling Exception for Arrays Beyond Limit (Contd.)
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.
Ver. 1.0 Session 14 Slide 7 of 30
8. 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.
Ver. 1.0 Session 14 Slide 8 of 30
9. 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
}
Ver. 1.0 Session 14 Slide 9 of 30
10. Object-Oriented Programming Using C#
Implementing Threads
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.
Ver. 1.0 Session 14 Slide 10 of 30
11. Object-Oriented Programming Using C#
Implementing Threads (Contd.)
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.
Ver. 1.0 Session 14 Slide 11 of 30
12. Object-Oriented Programming Using C#
Implementing Threads (Contd.)
The following figure shows a multithreaded process.
Ver. 1.0 Session 14 Slide 12 of 30
13. 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.
Ver. 1.0 Session 14 Slide 13 of 30
14. 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.
Ver. 1.0 Session 14 Slide 14 of 30
15. 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.
Ver. 1.0 Session 14 Slide 15 of 30
16. Object-Oriented Programming Using C#
Working with Threads
• 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.
Ver. 1.0 Session 14 Slide 16 of 30
17. Object-Oriented Programming Using C#
Creating Threads
• 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 = new
ThreadStart(ChildThreadCall);
Thread ChildThread = new Thread(ChildRef);
ChildThread.Start();
Ver. 1.0 Session 14 Slide 17 of 30
18. 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.
Ver. 1.0 Session 14 Slide 18 of 30
19. 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.
Ver. 1.0 Session 14 Slide 19 of 30
20. 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 state
The Runnable state
The Not Runnable state
The Dead state
Let us understand the life cycle of the thread with the help
of the following figure.
Ver. 1.0 Session 14 Slide 20 of 30
23. Object-Oriented Programming Using C#
Start() Started
Sleep() Wait/Join
Sleep
Interrupt()
Time Expires
Ver. 1.0 Session 14 Slide 23 of 30
24. Object-Oriented Programming Using C#
Start() Started
Thread Responds to
Abort() Stop Request Stop Request
Stopped
Ver. 1.0 Session 14 Slide 24 of 30
25. Object-Oriented Programming Using C#
The Unstarted State
• 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.
Ver. 1.0 Session 14 Slide 25 of 30
26. Object-Oriented Programming Using C#
The Runnable State
• 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.
Ver. 1.0 Session 14 Slide 26 of 30
27. Object-Oriented Programming Using C#
The Not Runnable State
A thread is not in the runnable state if it is:
Sleeping
Waiting
Blocked
Ver. 1.0 Session 14 Slide 27 of 30
28. 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.
Ver. 1.0 Session 14 Slide 28 of 30
29. Object-Oriented Programming Using C#
Summary
In this session, you learned that:
Exception handling is implemented using the following
keywords:
• try
• catch
• finally
– 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.
Ver. 1.0 Session 14 Slide 29 of 30
30. Object-Oriented Programming Using C#
Summary (Contd.)
– 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 state
Runnable state
Not Runnable state
Dead state
Ver. 1.0 Session 14 Slide 30 of 30
Notas del editor
Students have learnt the structure of different types of dimensions and the importance of surrogate keys in Module I. In this session, students will learn to load the data into the dimension tables after the data has been transformed in the transformation phase. In addition, students will also learn to update data into these dimension tables. Students already know about different types of dimension tables. Therefore, you can start the session by recapitulating the concepts. Initiate the class by asking the following questions: 1. What are the different types of dimensions? 2. Define flat dimension. 3. What are conformed dimension? 4. Define large dimension. 5. Define small dimension. 6. What is the importance of surrogate key in a dimension table? Students will learn the loading and update strategies theoretically in this session. The demonstration to load and update the data in the dimension table will be covered in next session.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Students have learnt the structure of different types of dimensions and the importance of surrogate keys in Module I. In this session, students will learn to load the data into the dimension tables after the data has been transformed in the transformation phase. In addition, students will also learn to update data into these dimension tables. Students already know about different types of dimension tables. Therefore, you can start the session by recapitulating the concepts. Initiate the class by asking the following questions: 1. What are the different types of dimensions? 2. Define flat dimension. 3. What are conformed dimension? 4. Define large dimension. 5. Define small dimension. 6. What is the importance of surrogate key in a dimension table? Students will learn the loading and update strategies theoretically in this session. The demonstration to load and update the data in the dimension table will be covered in next session.
Students have learnt the structure of different types of dimensions and the importance of surrogate keys in Module I. In this session, students will learn to load the data into the dimension tables after the data has been transformed in the transformation phase. In addition, students will also learn to update data into these dimension tables. Students already know about different types of dimension tables. Therefore, you can start the session by recapitulating the concepts. Initiate the class by asking the following questions: 1. What are the different types of dimensions? 2. Define flat dimension. 3. What are conformed dimension? 4. Define large dimension. 5. Define small dimension. 6. What is the importance of surrogate key in a dimension table? Students will learn the loading and update strategies theoretically in this session. The demonstration to load and update the data in the dimension table will be covered in next session.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Students know the importance of surrogate keys. In this session students will learn the strategy to generate the surrogate key. Give an example to explain the strategy to generate the surrogate keys by concatenating the primary key of the source table with the date stamp. For example, data from a Product table has to be loaded into the Product_Dim dimension table on Feb 09, 2006. The product_code is the primary key column in the Product table. To insert the surrogate key values before loading the data into the dimension table, you can combine the primary key value with the date on which the data has to be loaded. In this case the surrogate key value can be product_code+09022006.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Student already have learnt about type 2 SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 2 SCDs? Given an example to explain type 2 SCDs. This will recapitulate what they have learnt about type 2 SCD in Module 1. Now explain the strategy to update the data into these dimension tables with help the example given in SG. After explaining the examples, you can ask students to think of an example of a type 2 SCD and then tell the strategy to update the data into this dimension table.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.