0% found this document useful (0 votes)
2 views

T5_U5_Exception_Handling

The document discusses exception handling in programming, emphasizing its importance in identifying and managing errors during software development. It outlines the keywords 'try', 'throw', and 'catch' used in C++ for handling exceptions, providing syntax examples and explaining their functionality. Additionally, it contrasts a program without exception handling that results in an error message with a version that uses exception handling to provide a user-friendly error message.

Uploaded by

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

T5_U5_Exception_Handling

The document discusses exception handling in programming, emphasizing its importance in identifying and managing errors during software development. It outlines the keywords 'try', 'throw', and 'catch' used in C++ for handling exceptions, providing syntax examples and explaining their functionality. Additionally, it contrasts a program without exception handling that results in an error message with a version that uses exception handling to provide a user-friendly error message.

Uploaded by

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

Exception handling

· While writing large programs , a programmer usually makes many mistakes.

· Due to this , bugs occur even in the released softwares.

· Developing an error-free program is the only objective and intention of the programmer.

· Programmer should take care to prevent errors.

· Error can be trapped using exception-Handling features.

· A few programming languages support exception-handling features . without this feature , the
programmers should find out errors on their own.

· The error may be logical errors (or) syntax error.

· The logical error remains in the program due to a poor understanding of the program

· The Syntax Error made are due to a lack of understanding of the programming langauge

· C++ provides an Exception-Handling procedure to reduce the errors that the programmer
makes.

· The programmer always faces unusual errors while writing programs.

· An exception is an abnormal temination of the program, which is executed in a program at run


time or it may be called at run time when the error occurs.

· The exception contains warning messages such as

· invalid argument , insufficient memory , and division by zero

THE KEYWORDS try , throw & catch

· The exception - handling technique passes the control of a program from a location of exception
in a program to an exception-handler routine linked with the try lock .

· An exception - handler routine can only be called by the throw statement.

try

· The try keyword is followed by a series of statements enclosed in curly braces .

Syntax :

try

{
statement-1;

statment-2;

throw

· The function of the throw statement is to send the exception found .

· The declaration of the throw statement is as follow:

Syntax:

throw (except);

throw excep;

throw // re-throwing of an exception

catch

· Similar to the try block , the catch block also contains a series of statements enclosed in curly
braced.

· It also contains an argument of an exception type in parentheses.

syntax

try

statement-1

statement-2

catch(argument)

statement-3; // Action to be taken

· when an exception found , the catch block is executed.

· The catch statement contains an argument of exception type, and it is optional.


· When an argument is declared , the argument can be used in the catch block.

· After execution of the catch block the statements inside the blocks are executed.

· In case no exception is caught , the catch block is ignored, and if a mismatch is found , the
program is terminated

Write a program without exception

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int x=5,y=0,c;

c=x/y;

cout<<"\n Division is:"<<c;

getch();

OUTOUT

division by zero

· In the above program when user divided any number by zero means the output will throw the
error like as above , but as a user it will not understandable for the user

· A programmer can solve the error with the help of exception handling

Write the above program using Exception handling

#include<iostream.h>

#include<conio.h>

void main()

clrscr();
int x=5,y=0,c=0;

try

if(y==0)

throw " Don't user Zero as a divisior";

else

c=x/y;

cout<<"\n Division is:"<<c;

catch(char* msg)

cout<<msg;

getch();

· In above program if x=5 and y=0 means the if statement become true

· so try will identify the error and throw the message "Dont use zero as a divisor" with the help of
throw keyword

· catch- block will receive that upcomming error from try block with the help of string argument
varaible( msg) and display the erro to the user.

You might also like