Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct Exceptions in C++

I am just learning how to handle errors in my C++ code. I wrote this example that looks for a text file called some file, and if its not found will throw an exception.

#include <iostream> #include <fstream> using namespace std;  int main() { int array[90]; try {    ifstream file;    file.open("somefile.txt");    if(!file.good())     throw 56; } catch(int e) {     cout<<"Error number "<<e<<endl; } return 0; } 

Now I have two questions. First I would like to know if I am using Exceptions correctly. Second, (assuming the first is true) what is the benefit to using them vs an If else statement?

like image 347
Dr.Ackula Avatar asked Apr 27 '10 04:04

Dr.Ackula


People also ask

What are exceptions in C?

C # in Telugu An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another.

What are the 3 types of exceptions?

There are three types of exception—the checked exception, the error and the runtime exception.

How do you handle exception in C?

As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno.

Can we handle exception in C?

C doesn't support exception handling.


2 Answers

"Correctly" is a value judgment, but (unlike other classes) there's a major benefit from exceptions classes being a monolithic hierarchy, so I'd generally advise throwing something derived from std::exception, not simply an int.

Second, it's open to question whether an incorrect file name is sufficiently unexpected to qualify as a good reason to throw an exception at all.

As to benefits vs. an if/else statement: there are a couple. First, exceptions let you segregate the code that deals with errors, so the main idea and readability of the code don't get lost in a maze of error handling. Second, when you have several layers of code between throwing and catching the exception, the code that throws the exception may not know how it should really be handled. Your code, for example, uses std::cout to report the problem -- but most such code would report errors on std::cerr instead. You can change from one to the other without any change to the code that tried to open the file (which might be deep in a library, and have no clue of which should be used for this application -- and might be used in an application where both are wrong, and MessageBox was preferred).

like image 135
Jerry Coffin Avatar answered Sep 23 '22 19:09

Jerry Coffin


First I would like to know if I am using Exceptions correctly.
Yes, though generally you want your exceptions to derive from std::exception.

Second, (assuming the first is true) what is the benefit to using them vs an If else statement?
For the given example, nothing. The benefit of exceptions comes when you have many deep nested functions, like this.

#include <stdexcept> #include <iostream> #include <string>  void anErrorFunc(const std::string& x) {     ifstream file;     file.open(x);     if (!file)         throw std::runtime_error("Could not open file"); }  void someOtherFunction(const std::string& y) {     //Do stuff     anErrorFunc(y);     //Do other stuff }  int main() {     try {         someOtherFunction("somefile.txt");     } catch (std::exception &ex) {         std::cout << "Ouch! That hurts, because: "             << ex.what() << "!\n";     } } 

Note that the exception will be caught in main(), and someOtherFunction does not have to worry about dealing with passing through failure return codes.

like image 33
Billy ONeal Avatar answered Sep 21 '22 19:09

Billy ONeal