Implicit and explicit sequence control with exception handling

Post on 20-May-2015

2.039 views 1 download

Tags:

description

Focus on Exception handling... in C# with Implicit and Explicit sequence control By Vikash Mainanwal

Transcript of Implicit and explicit sequence control with exception handling

A Presentation 0f Advance Pr0gramming Language

Implicit & Explicit Sequence Control

and Exception Handling

Master of TechnologyIn

Computer Science &Engineering Submitted T0 Submitted By

Miss. Manshi Gupta Vikash MainanwaI

Control Structure Sequence Control Structure Implicit Explicit Exception Exception Handling Bibliography

CONTENTS

Control structures: the basic framework within which operations and data are combined into programs. Sequence control data control

1.Control Structure

the control of the order of execution of the operations (Primitive, user defined).

1.1 Sequence control

1.2 Data control

the control of the transmission of data among the

subprograms of a program.

Implicit sequence control: (default) defined by the language .

Explicit sequence control: defined by the programmer.

Types of sequence control

Example of Explicit Sequence control

Describe Exception

An exception is an erroneous situation that occurs during program execution.

When an exception occurs in an application, the system throws an error.

The error is handled through the process of exception handling.

Types of Error

There are three types of errors that can occur in the application: Syntax errors: Occurs when statements are not constructed properly, keywords are misspelled, or punctuation is omitted.

Run-time errors: Occurs when an application attempts to perform an operation, which is not allowed at runtime.

Logical errors: Occurs when an application compiles and runs properly but does not produce the expected results.

Let us understand the various types of errors in detail.

Syntax Error

class Errors

{

Console.WriteLine(“Enjoy Errors”)

}

Semicolon is missing from Console.WriteLine statement

Run Time Error

class Errors

{

int Num1=0;int Num2=20;int Num3;

Num3=Num2/Num1;

Console.WriteLine(“The Result is {0}”, Num3);

}

Division by zero has taken place

Logical Error

class Errors

{

int Num1=10;int Num2=2;int Num3;

Num3=Num2/Num1;

Console.WriteLine(“The Result is {0}”, Num3);

}

Expected result = 5

Present result = 0.2

There are many exception classes which are directly or indirectly derived from the System.Exception class. Some of these classes are:

System.ApplicationException class

System.SystemException class

Exception Classes

� The hierarchy of the exception classes is displayed in the following figure.

Exception Classes

System. Exception

System.ApplicationException

System.SystemException

Handling ExceptionIn 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.

Exception Handling Control Flow

Try BlockThe 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.

Catch Block

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

}

Finally Block

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

}

Example of Exception Handling

Bibliography• Programming Language Terrance W.Pratt• � www.dotnetperls.com/exception• �

msdn.microsoft.com/en-us/library/ms173160.aspx

�www.tutorialspoint.com/csharp/csharp_exception_handling.

• The complete reference C# 4.0 Tata McGraw-Hill