0% found this document useful (0 votes)
38 views7 pages

C++ Exception Handling: Advantage

Exception handling in C++ allows the normal flow of a program to continue even if runtime errors occur. Exceptions are objects that are thrown when errors happen and can be caught using try/catch blocks. Common exception classes in C++ inherit from the std::exception class. Keywords like try, catch, and throw are used to handle exceptions. Files can be read from and written to using file streams like ifstream and ofstream.

Uploaded by

Rohan Saki
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)
38 views7 pages

C++ Exception Handling: Advantage

Exception handling in C++ allows the normal flow of a program to continue even if runtime errors occur. Exceptions are objects that are thrown when errors happen and can be caught using try/catch blocks. Common exception classes in C++ inherit from the std::exception class. Keywords like try, catch, and throw are used to handle exceptions. Files can be read from and written to using file streams like ifstream and ofstream.

Uploaded by

Rohan Saki
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/ 7

C++ Exception Handling

Exception Handling in C++ is a process to handle runtime errors. We perform exception


handling so the normal flow of the application can be maintained even after runtime errors.

In C++, exception is an event or object which is thrown at runtime. All exceptions are
derived from std::exception class. It is a runtime error which can be handled. If we don't
handle the exception, it prints exception message and terminates the program.

Advantage
It maintains the normal flow of the application. In such case, rest of the code is executed
even after exception.

C++ Exception Classes


In C++ standard exceptions are defined in <exception> class that we can use inside our
programs. The arrangement of parent-child class hierarchy is shown below:
All the exception classes in C++ are derived from std::exception class. Let's see the list of
C++ common exception classes.

Exception Description

std::exception It is an exception and parent class of all standard C++ exceptions.

std::logic_failure It is an exception that can be detected by reading a code.

std::runtime_error It is an exception that cannot be detected by reading a code.

std::bad_exception It is used to handle the unexpected exceptions in a c++ program.

std::bad_cast This exception is generally be thrown by dynamic_cast.

std::bad_typeid This exception is generally be thrown by typeid.


std::bad_alloc This exception is generally be thrown by new.

C++ Exception Handling Keywords


In C++, we use 3 keywords to perform exception handling:

o try

o catch, and

o throw

Moreover, we can create user-defined exception which we will learn in next chapters.

C++ try/catch
In C++ programming, exception handling is performed using try/catch statement. The C+
+ try block is used to place the code that may occur exception. The catch block is used to
handle the exception.

C++ example without try/catch


1. #include <iostream>  
2. using namespace std;  
3. float division(int x, int y) {  
4.    return (x/y);  
5. }  
6. int main () {  
7.    int i = 50;  
8.    int j = 0;  
9.    float k = 0;  
10.       k = division(i, j);  
11.       cout << k << endl;  
12.    return 0;  
13. }  

Output:

Floating point exception (core dumped)


C++ try/catch example
1. #include <iostream>  
2. using namespace std;  
3. float division(int x, int y) {  
4.    if( y == 0 ) {  
5.       throw "Attempted to divide by zero!";  
6.    }  
7.    return (x/y);  
8. }  
9. int main () {  
10.    int i = 25;  
11.    int j = 0;  
12.    float k = 0;  
13.    try {  
14.       k = division(i, j);  
15.       cout << k << endl;  
16.    }catch (const char* e) {  
17.       cerr << e << endl;  
18.    }  
19.    return 0;  
20. }  

Output:

Attempted to divide by zero!

C++ Files and Streams


In C++ programming we are using the iostream standard library, it
provides cin and cout methods for reading from input and writing to output respectively.

To read and write from a file we are using the standard C++ library called fstream. Let us
see the data types define in fstream library is:

Data Type Description

fstream It is used to create files, write information to files, and read information from files.

ifstream It is used to read information from files.


ofstream It is used to create files and write information to the files.

C++ FileStream example: writing to a file


Let's see the simple example of writing to a text file testout.txt using C++ FileStream
programming.

1. #include <iostream>  
2. #include <fstream>  
3. using namespace std;  
4. int main () {  
5.   ofstream filestream("testout.txt");  
6.   if (filestream.is_open())  
7.   {  
8.     filestream << "Welcome to javaTpoint.\n";  
9.     filestream << "C++ Tutorial.\n";  
10.     filestream.close();  
11.   }  
12.   else cout <<"File opening is fail.";  
13.   return 0;  
14. }  

Output:

The content of a text file testout.txt is set with the data:


Welcome to javaTpoint.
C++ Tutorial.

C++ FileStream example: reading from a file


Let's see the simple example of reading from a text file testout.txt using C++ FileStream
programming.

1. #include <iostream>  
2. #include <fstream>  
3. using namespace std;  
4. int main () {  
5.   string srg;  
6.   ifstream filestream("testout.txt");  
7.   if (filestream.is_open())  
8.   {  
9.     while ( getline (filestream,srg) )  
10.     {  
11.       cout << srg <<endl;  
12.     }  
13.     filestream.close();  
14.   }  
15.   else {  
16.       cout << "File opening is fail."<<endl;   
17.     }  
18.   return 0;  
19. }  

Note: Before running the code a text file named as "testout.txt" is need to be created and
the content of a text file is given below:

Welcome to javaTpoint.

C++ Tutorial.

Output:

Welcome to javaTpoint.
C++ Tutorial.

C++ Read and Write Example


Let's see the simple example of writing the data to a text file testout.txt and then reading
the data from the file using C++ FileStream programming.

1. #include <fstream>  
2. #include <iostream>  
3. using namespace std;  
4. int main () {  
5.    char input[75];  
6.    ofstream os;  
7.    os.open("testout.txt");  
8.    cout <<"Writing to a text file:" << endl;  
9.    cout << "Please Enter your name: ";   
10.    cin.getline(input, 100);  
11.    os << input << endl;  
12.    cout << "Please Enter your age: ";   
13.    cin >> input;  
14.    cin.ignore();  
15.    os << input << endl;  
16.    os.close();  
17.    ifstream is;   
18.    string line;  
19.    is.open("testout.txt");   
20.    cout << "Reading from a text file:" << endl;   
21.    while (getline (is,line))  
22.    {  
23.    cout << line << endl;  
24.    }      
25.    is.close();  
26.    return 0;  
27. }  

Output:

Writing to a text file:


Please Enter your name: Nakul Jain
Please Enter your age: 22
Reading from a text file: Nakul Jain
22

You might also like