C++ Exception Handling

Posted by user1413793 on Stack Overflow See other posts from Stack Overflow or by user1413793
Published on 2012-06-07T04:29:35Z Indexed on 2012/06/07 4:40 UTC
Read the original article Hit count: 284

So I was writing some code and I noticed that apart from syntactical, type, and other compile-time errors, C++ does not throw any other exceptions. So I decided to test this out with a very trivial program:

#include<iostream>

int main() {
    std::count<<5/0<<std::endl;
return 1
}

When I compiled it using g++, g++ gave me a warning saying I was dividing by 0. But it still compiled the code. Then when I ran it, it printed some really large arbitrary number. When I want to know is, how does C++ deal with exceptions? Integer division by 0 should be a very trivial example of when an exception should be thrown and the program should terminate.

Do I have to essentially enclose my entire program in a huge try block and then catch certain exceptions? I know in Python when an exception is thrown, the program will immediately terminate and print out the error. What does C++ do? Are there even runtime exceptions which stop execution and kill the program?

© Stack Overflow or respective owner

Related posts about c++

Related posts about exception