newbie questions about c++ exceptions

i was reading about C++ exceptions on this site and i can't get the point of it. i mean why use exceptions when i can just use if statements with cout or break to handle certain errors? why use exceptions instead of if statements and what's the benefit of it? i can't seem to get why i need to use it.
Exceptions are what takes care of when something unusual (but not necessarily unexpected) happens. A very good example is when you are trying to allocate memory with new and the allocation fails for some reason. The computer will immediately ignore all normal code, such as if (...) statements. In the case of error handling, the memory allocation failure will throw an exception, and you can catch that exception with a try {...} catch(); and then take appropriate actions, such as outputting to an error log, before the program terminates. It will always terminate if you get a bad memory allocation, but there are other cases where you can handle things more gracefully, and if you catch a bad allocation, you can, again, take other steps before the termination of the program...like terminating gracefully instead of simply crashing.
@ciphermagi Good explanation.

@jinjin12 Another scenario, which might make sense with your knowledge of the language is when doing something as simple as opening a file to read. If the file does not exist, an exception "File not found" will be thrown. Under normal conditions you would open the file then process it using your normal if statements with cout or break to handle certain errors.
@ciphermagi
1) so you're saying that the program automatically shuts down memory allocation fails? i not that experienced with the new operator so can you tell me an example of how memory allocation with the new keyword can fail?
2) you said if it fails, an exception is thrown? this exception is thrown automatically? or do i have to write the throw code. i know it's the former but i just want to be sure.



@clanmjc, i can see why i would need to use exceptions for the example ciphermagi gave, but i don't see why i would need to use exceptions for the example you gave when i can just use an if statement to just if the file exists or not, and then properly handle the situation, which i have done before
) so you're saying that the program automatically shuts down memory allocation fails?


He's saying that an exception is thrown. Whether that results in the program shutting down or not is up to code you write.


can you tell me an example of how memory allocation with the new keyword can fail?


Memory is a finite resource. My guess is that when there's no more to allocate, the allocation will fail.


you said if it fails, an exception is thrown? this exception is thrown automatically?


Yes.

I agree that Clanmjc's example is poor. A common usage is in checked accessor functions (such as the 'at' method of vectors). How does one report an error constructing an object? Often, by throwing an exception from the constructor. Anywhere a class invariant is violated is a good candidate for an exception being thrown.

He's saying that an exception is thrown. Whether that results in the program shutting down or not is up to code you write.


No...I'm saying that when dynamic memory allocation fails, the program has to terminate. Whether it terminates gracefully or crashes is up to the code you write.
Then you're saying something that ain't true.
I agree that Clanmjc's example is poor
I agree that the example is poor, but based on the posters assumption that if statements could be used just as exceptions were, made me think that the posters knowledge of how exceptions occurring at all was very "simple". So I used a poor, but so simple that a first year student could see how something bad could happen with that example. Of course you could use if statement in this case, but if you tried to open the file for writing and it was read only, or you tried to open the file from a service account that did not have write access to the file that is owned by some other account an exception would be thrown and no if, case statement, could handle this, the program would exit.

Here is a more concrete example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class BaseOne
{
   //....
}

class BaseTwo
{
   //...
}

class DerivedTwo : public BaseTwo
{
  //...
}


Later in the code this function takes a reference to the BaseOne class but then tries to cast to the DerivedTwo which inherits from BaseTwo, throws bad cast exception
1
2
3
4
5
void SomeFunctions(const BaseOne& obj)
{
    dynamic_cast<const DerivedTwo &>(obj); //Throws bad_cast exception
}


The side effect of Line 3 in the function code block can only be handled through a catch.

Exceptions are used for runtime errors (errors that go beyond the scope of logical errors, exceptional, abnormal, extraordinary, heteromorphic, irregular errors, but they can be anticipated), conditional statements are used by 'logical errors' for lack of a better word.
Last edited on
@cire:

Sorry, I went back and re-read. Memory allocation errors that aren't handled cause infinite loops. I was thinking of unhandled exceptions.

Which, if you're still paying attention, OP, means that if you use some sort of STL function or a library that you didn't create that does throw exceptions, and you don't handle them...your program ends.
Last edited on
Topic archived. No new replies allowed.