UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction Program errors are also referred to as program...

13
UNIT 3 TEMPLATE AND EXCEPTION HANDLING

Transcript of UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction Program errors are also referred to as program...

Page 1: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

UNIT 3 TEMPLATE AND EXCEPTION

HANDLING

Page 2: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Introduction

Program errors are also referred to as program bugs.

A C program may have one or more of four types of errors: Syntax errors (Compiler errors or Compile-

time errors) Linker Errors Runtime errors Logic errors

Page 3: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Syntax errors

Syntax errors represent grammar errors in the use of the programming language.  Common examples are: Misspelled variable and function names Missing semicolons Unmatched parentheses, square brackets, and curly

braces Using a variable that has not been declared Incorrect format in selection and loop statements

Syntax errors are the easiest to find and fix.

Page 4: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Linker errors

Linker errors are generated when the linker encounters what looks like a function call; but it cannot find a function with that name. This is usually caused by misspelling a C standard function (like main) or not including the header file for a function.

Example1. int main()2. {3. add(); //function call4. return 0;5. }

Page 5: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Runtime errors

A type of error that occurs during the execution of a program is known as run-time error. Runtime errors may crash your program when you run it. Runtime errors occur when a program with no syntax errors directs the computer to execute an illegal operation. Common examples are: Trying to divide by a variable that contains a

value of zero Trying to open a file that does not exist

Page 6: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Runtime error

There is no way for the compiler to know about these kinds of errors when the program is compiled. Runtime errors are commonly due to wrong input from the user.

Runtime errors are usually more difficult to find and fix than syntax errors.

To find the source of a run-time error in a program, usually a software called debugger is used.

Page 7: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Logic errors Logic errors occur when a programmer implements the

algorithm for solving a problem incorrectly. A statement with logical error may produce unexpected and wrong results in the program.

Common examples are: Multiplying when you should be dividing Adding when you should be subtracting Opening and using data from the wrong file Displaying the wrong message

Logic errors are the hardest to find and fix because: The compiler does not detect these errors There is no indication of error when the program is executed. The program may produce correct results for some input data

and wrong results for other input data.

Page 8: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Exception

An exception is a problem that arises during the execution of a program.

A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

If exceptions occurred during execution Programmer-supplied code terminated the program or Program terminated with an appropriate error

message

Page 9: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.

C++ exception handling is built upon three keywords: try, catch, and throw.

try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

throw: A program throws an exception when a problem shows up. This is done using a throw keyword.

Page 10: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Syntax

1. try2. { 3. // protected code 4. }5. catch( exceptionname e1 ) 6. { // catch block }7. catch( exceptionname e2 ) 8. { // catch block }9. catch( exceptionname en ) 10. { // catch block }

You can list down multiple catch statements to catch different type of exceptions in case your try block raises more than one exception in different situations.

Page 11: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Exception Handler

To catch exceptions we have to place our code on which we want exception handling in the try block. If an exception occurs the control is passed to the handler, otherwise the handlers are ignored.

The code to be executed, that may produce exceptions, is placed in the try block and the error handlers are declared with the keyword catch.

Page 12: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

Example

Step 1: Start the program. Step 2: Declare the variables a,b,c. Step 3: Read the values a,b,c,. Step 4: Inside the try block check the condition.      a. if(a-b!=0) then calculate the value of d

and display.      b. otherwise throw the exception. Step 5: Catch the exception and display the

appropriate message. Step 6: Stop the program.

Page 13: UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.

1. #include<iostream.h>

2. using namespace std;

3. int main()

4. {

5.    int a,b,c;

6.    float  d;

7.    clrscr();

8.    cout<<"enter the value of a:";

9.    cin>>a;

10.    cout<<"enter the value of b:";

11.    cin>>b;

12.    cout<<"enter the value of c:";

13.    cin>>c;

14. try15.    {16.               if((a-b)!=0)17.               {18.                  d=c/(a-b);19.                  cout<<"result is:"<<d;20.               }21.               else22.               {23.                  throw(a-b);24.               }25.    }26.  catch(int i)27.    {28.               cout<<"answer is infinite

because a-b is:"<<i;29.    }30. return 0;  31. } //end of main

Output: Enter the value for a: 20              Enter the value for b: 20              Enter the value for c: 40                           Answer is infinite because a-b is: 0