Implicit and explicit sequence control with exception handling

22
Presentation 0f Advance Pr0gramming Language Implicit & Explicit Sequence Control and Exception Handling Master of Technology In Computer Science &Engineering Submitted T0 Submitted By Miss. Manshi Gupta Vikash MainanwaI

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

Page 1: 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

Page 2: Implicit and explicit sequence control with exception handling

Control Structure Sequence Control Structure Implicit Explicit Exception Exception Handling Bibliography

CONTENTS

Page 3: Implicit and explicit sequence control with exception handling

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

1.Control Structure

Page 4: Implicit and explicit sequence control with exception handling

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.

Page 5: Implicit and explicit sequence control with exception handling

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

Explicit sequence control: defined by the programmer.

Types of sequence control

Page 6: Implicit and explicit sequence control with exception handling

Example of Explicit Sequence control

Page 7: Implicit and explicit sequence control with exception handling

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.

Page 8: Implicit and explicit sequence control with 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.

Page 9: Implicit and explicit sequence control with exception handling

Syntax Error

class Errors

{

Console.WriteLine(“Enjoy Errors”)

}

Semicolon is missing from Console.WriteLine statement

Page 10: Implicit and explicit sequence control with exception handling

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

Page 11: Implicit and explicit sequence control with exception handling

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

Page 12: Implicit and explicit sequence control with exception handling

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

Page 13: Implicit and explicit sequence control with exception handling

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

Exception Classes

System. Exception

System.ApplicationException

System.SystemException

Page 14: Implicit and explicit sequence control with exception handling

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.

Page 15: Implicit and explicit sequence control with exception handling

Exception Handling Control Flow

Page 16: Implicit and explicit sequence control with exception handling

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.

Page 17: Implicit and explicit sequence control with exception handling

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

}

Page 18: Implicit and explicit sequence control with exception handling

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

}

Page 19: Implicit and explicit sequence control with exception handling

Example of Exception Handling

Page 20: Implicit and explicit sequence control with 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

Page 21: Implicit and explicit sequence control with exception handling
Page 22: Implicit and explicit sequence control with exception handling