[go: up one dir, main page]

0% found this document useful (0 votes)
9 views17 pages

EXCEPTION

The document discusses errors in C++, categorizing them into three types: syntax errors, logical errors, and runtime errors. It explains exception handling, which allows programs to manage unexpected situations during execution using the keywords try, catch, and throw. An example illustrates how dividing by zero can trigger an exception, demonstrating the importance of handling errors in programming.

Uploaded by

SUMITKUMAR MAHTO
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)
9 views17 pages

EXCEPTION

The document discusses errors in C++, categorizing them into three types: syntax errors, logical errors, and runtime errors. It explains exception handling, which allows programs to manage unexpected situations during execution using the keywords try, catch, and throw. An example illustrates how dividing by zero can trigger an exception, demonstrating the importance of handling errors in programming.

Uploaded by

SUMITKUMAR MAHTO
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/ 17

Errors in c++

Errors refers to a mistake


in the code that prevents
the program from
compiler or running
correctly.
There are basically three
types of errors :-
1. Syntax error
2. Logical error
3. Run time error
Syntax error: These are the
errors which occur due to the
wrong syntax of the code.
Ex: for(i=1,i<=10,i++)
Here, it will show syntax
error because we have used
“,” in the place of “;” which is
against the syntax of the
code.

Logical error: These are the


errors which occur due to the
wrong logic of the program.
Ex: dividing by 0, or taking
c=a*b; in the programming of
addition of two numbers, etc.
Run time error: These are the
errors which occurs while the
program is running.
Run time errors are also
known as Exception errors.
real time example of
exception handling:

Suppose a man’s car got


punctured. He changed the
punctured tire with a new tire.
After changing the tire, he
continues the rest of the journey.

Exception handling
Exception handling is the
unexcepted / unwanted /
abnormal situation that
occurred at the run time .

Exception handling provides


us the facility to handle the
exception so that our program
keeps running.

These happens during running


time of the program.
C++ exception handling is
built upon three keywords:
try , catch , throw.
Throw: A Program throws an
exception when a problem
shows up. This is done using a
throw keyword. Exceptions
can be thrown anywhere
within a code block using
throw statement.

catch: A Program catches an


exception with an exception
handler at the place in a
program where you want to
handle the program.

The catch block


following the try catches any
exception. You can specify
what type of exception you
want to catch ant this is
determined by the exception
declaration that appears in
parenthesis following the
keyword catch.

Try: A try identifies a block of


code for which particular
exceptions will be acticated. It
is followed by one or more
catch block.

Syntax:

try{
//protected code
}catch( exception e1 ){
//catch block
}catch( exception e2 ){
//catch block
}catch( exception eN ){
//catch block
}

Program example:
If the value of z = 0 then it was
sure that the program would
have been thrown an exception,
as anything divided by 0 gives
indefinite value and gives an run
time error.
Then an exception message
would have been appeared as
an output which is declared in
the catch and the program
would have been ended up.

You might also like