0% found this document useful (0 votes)
5 views6 pages

BSC Mpcs 2nd Sem Unit 4

The document discusses exception handling in C++, explaining the types of errors: compile-time errors (syntax errors) and run-time errors (logical errors). It details how to handle exceptions using the keywords 'try', 'catch', and 'throw', providing examples for each. Additionally, it describes how exceptions can accept arguments and how multiple catch statements can be used for different parameter types.

Uploaded by

MS Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

BSC Mpcs 2nd Sem Unit 4

The document discusses exception handling in C++, explaining the types of errors: compile-time errors (syntax errors) and run-time errors (logical errors). It details how to handle exceptions using the keywords 'try', 'catch', and 'throw', providing examples for each. Additionally, it describes how exceptions can accept arguments and how multiple catch statements can be used for different parameter types.

Uploaded by

MS Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT 4

EXCEPTION HANDLING
EXCEPTION:

Rarely does a program run successfully at first attempt it is common to make mistake while
developing as well as typing a program. To produce expected errors, result errors than to go a
wrong program. Errors and abnormal conditions arising while executing a program its type of error
is called as “Exception”.
Types of Errors (Exceptions):

In C++ programming language, errors may be identified into two types. They are
1. Compile-time Errors (Syntax Errors).
2. Run-time Errors (Logical Errors).
1. Compile-time Errors (Syntax Errors):
All syntax errors detected and displayed by the C++ compiler. Therefore these errors are
known as “Compile-time Errors”.
The most common errors are listed below.
i. Missing semicolons.
ii. Missing or mismatch of brackets
iii. Misspelling identifiers and keywords.
iv. Missing double quotes in a string.
v. Use of undeclared variables.
Example:

#include<iostream.h>
Void main( )
{
int a,b,c;
cout<<”enter a,b values”;
cin>>a>>b;
c=a+b
cout<<”c values is”<<c;
}
The above program saves as “addition.cpp”. After saving a program than we can compile
the above program, to display one error. Its error as follow “semicolon is missing”.
2. Run-time Errors (Logical Errors):
In C++, after compiling the C++ program, there is no compile time error, than we can run it. In
that time running a program to display the some errors are known as Run-time Errors.
Reasons for Run-time Errors Occurrence:

i. Division-by zero condition


ii. Exceeding the bounds of an array
iii. Running out of memory
iv. Object initialization to an impossible value.
Example:
#include<iostream.h>
Void main( )

{
int a=10,b=0,c;
c=a/b;
cout<<”c values is”<<c;
}
The above program saves as “division.cpp”. after saving we compile it. In compiling, there are

any no errors. That means compiling is successfully. After than we can run it program. But in run the
program, the program to display the error. That is “cannot divisible by zero”

Exception Handling:

In C++, a programme having some Exception (Errors), I that time we cannot handle the
program, that means a program is a abnormal condition which occurs during execution of program
when C++ handled that type of exception program by using Exception handling.
In C++ handled Exception by using 3 keywords.
• try
• catch
• throw

1. Try:
Try is a keyword that is used to detect the exception i.e., run-time error. The statements that may
cause exception are saved inside the try block. The general syntax of try block is as follow
try
{

Statements of code;
------------------------
Throw exception;
2. Catch: }
The catch block handles an exception thrown by the try block. It defines an action to be
taken when a run-time error occurs. The general syntax of catch block as follows

catch(type arguments)

{
Statement;

A catch block takes arguments as an exception. These arguments specify the type of an exception
that can be handled by the catch block. When an exception is thrown the control goes to catch
block. If the type of an exception thrown matches the argument then the catch block is executed,
otherwise the program terminates abnormally. If no exception is thrown from the try block then
the catch block is skipped and control goes immediately to the next statement following the catch
block.
3. Throw:
An exception detected in try block is thrown using “throw” keyword. An exception can be thrown
using throw statement in following number if ways.
throw(exception);
throw exception;
throw;
Example:
#include <iostream.h>
void main()
{
int a=10, b=0, c;
try
{
if(b == 0)
{
throw "Division by zero not possible";
c = a/b;
}
}
catch(char* ex)
{
cout<<ex;
}
}
Exception with Arguments:

Exception handler is responsible for handling exceptional situations that occur in the
program. In order to catch the exceptions the related code must be placed in try block with this the
exception will be thrown in exception conditions. The control will be transferred to the exception
handler.
The throw exception is capable of accepting a parameter that is passed as arguments to
exception handler. An exception handler can be declared using catch keyword which contains at
least one parameter. The type of argument in catch statement should be same as that parameter
used in throw statement. Otherwise the exception will not be caught. To avoid multi[le exception
handlers can be used. The different catch statements are used which in turn make use of different
parameter types.
Example:

#include<iostream.h>
#include<string.h>
Void main( )
{
int a,b,c;
cout<<”enter the a,b values”;
cin>>a>>b;
try
{
if(b==0)
{
throw b;
}
if(b<0)
{
throw “negative denominator not allowed”;
}
c=a/b;
cout<<”the value of C is :”<<c
}
catch(int num)
{
Cout<<”we canot enter”<<num<<:in denominator”;
}
}

You might also like