0% found this document useful (0 votes)
157 views13 pages

Exception Handling

Exception handling allows C++ programs to handle runtime errors or abnormal conditions gracefully. There are two types of exceptions: synchronous and asynchronous. C++ provides try, catch, and throw keywords to handle exceptions. The try block contains code that may throw an exception, catch blocks define code to handle specific exceptions, and throw throws an exception. Exceptions can be re-thrown and blocks can be nested to partially handle exceptions.

Uploaded by

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

Exception Handling

Exception handling allows C++ programs to handle runtime errors or abnormal conditions gracefully. There are two types of exceptions: synchronous and asynchronous. C++ provides try, catch, and throw keywords to handle exceptions. The try block contains code that may throw an exception, catch blocks define code to handle specific exceptions, and throw throws an exception. Exceptions can be re-thrown and blocks can be nested to partially handle exceptions.

Uploaded by

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

Exception handling

• One of the advantages of C++ over C is Exception


Handling.
• Exceptions are run-time anomalies or abnormal
conditions that a program encounters during its
execution.
• There are two types of exceptions: a)Synchronous,
• b)Asynchronous
• (Ex:which are beyond the program’s control, Disc
failure etc).
• C++ provides following specialized keywords for this
purpose.
• try: represents a block of code that can throw
an exception.
• Catch: represents a block of code that is
executed when a particular exception is
thrown.
• throw: Used to throw an exception. Also used
to list the exceptions that a function throws,
but doesn’t handle itself.
• #include <iostream>
• using namespace std;

• int main()
• {
• int x = -1;

cout << "Before try \n";


• try {
• cout << "Inside try \n";
• if (x < 0)
• {
• throw x;
• cout << "After throw (checking it will not execute) \n";
• }
• }
• catch (int x ) {
• cout << "Exception Caught \n";
• }

• cout << "After catch (Will be executed) \n";


• return 0;
• }
• There is a special catch block called ‘catch all’
catch(…) that can be used to catch all types of
exceptions. For example, in the following
program, an int is thrown as an exception, but
there is no catch block for int, so catch(…)
block will be executed.
example
• #include <iostream>
• using namespace std;

• int main()
• {
• try {
• throw 10;
• }
• catch (char *excp) {
• cout << "Caught " << excp;
• }
• catch (...) {
• cout << "Default Exception\n";
• }
• return 0;
• }
• Implicit type conversion doesn’t happen for
primitive types. For example, in the following
program ‘a’ is not implicitly converted to int
• #include <iostream>
• using namespace std;

• int main()
• {
• try {
• throw 'a';
• }
• catch (int x) {
• cout << "Caught " << x;
• }
• catch (...) {
• cout << "Default Exception\n";
• }
• return 0;
• }
• If an exception is thrown and not caught
anywhere, the program terminates
abnormally. For example, in the following
program, a char is thrown, but there is no
catch block to catch a char.
• #include <iostream>
• using namespace std;

• int main()
• {
• try {
• throw 'a';
• }
• catch (int x) {
• cout << "Caught ";
• }
• return 0;
• }
• C++, try-catch blocks can be nested. Also, an
exception can be re-thrown using “throw; ”
• #include <iostream>
• using namespace std;

• int main()
• {
• try {
• try {
• throw 20;
• }
• catch (int n) {
• cout << "Handle Partially ";
• throw; //Re-throwing an exception
• }
• }
• catch (int n) {
• cout << "Handle remaining ";
• }
• return 0;
• }

You might also like