0

I am new to C++ and wondering how I could catch all possible errors that could happen in my program during run-time. This is for debugging reasons only. To solve possible errors, I would like to have a look at them first.

This would be my idea of how to catch possible errors in a program. I do not throw exceptions, but would like to catch possible errors directly.

#include <iostream>
#include <exception>
#include <stdexcept>

int doBadStuf(int i)
{
    // go out of bounce, or make other mistakes
    return 10 / i; 
}

int main()
{
    try
    {
        int i = doBadStuf(0);
    }
    catch (std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }
    catch (...)
    {
        std::cerr << "something else";
    }
}
User12547645
  • 6,955
  • 3
  • 38
  • 69
  • 1
    You can only catch exceptions using this approach, there is no way to detect or catch undefined behavior. The reason you cannot detect such behavior is because the behavior is *undefined*, therefore you cannot even know what symptom you should be looking for. – Cory Kramer Apr 23 '18 at 17:38
  • Many errors in c++ lead to undefined behavior, which are by their nature impossible to handle after the fact. – François Andrieux Apr 23 '18 at 17:39
  • Possible duplicate : https://stackoverflow.com/questions/315948/c-catching-all-exceptions – NiVeR Apr 23 '18 at 17:39
  • @NiVeR That question is asking about catching all exceptions. From the example, it's this question is asking for a way to catch all errors, even those that won't necessarily throw exceptions. – François Andrieux Apr 23 '18 at 17:40
  • If I compile this with g++ -std=c++11 tmp.cpp -o a.exe and then execute a.exe my "program" will crash without printing out an error. – User12547645 Apr 23 '18 at 17:40
  • @NiVeR I had a look at that post first. I implemented a "solution" as suggested in the post. Unfortunately it does not print the error, though. – User12547645 Apr 23 '18 at 17:42
  • @User12547645 Exceptions are a distinct system in c++ and only apply to those errors that are specifically said to throw exceptions. They are not compatible with all errors in general and cannot be used to implicitly detect errors such as division by zero. – François Andrieux Apr 23 '18 at 17:43
  • 1
    Best way is probably to run a debugger and just see where the crash occured. – Carl Apr 23 '18 at 17:43
  • Note that catching unexpected issues like this in main is sometimes not great - its best to let the OS do its thing and create a crash dump (Windows) or core dump (Linux) and analyze that - the reason being its a snapshot when thigs went wrong instead of sometime later. – Mike Vine Apr 23 '18 at 17:43
  • @FrançoisAndrieux I agree! In reality you do not throw exceptions all the time. Sometimes you just forget about possible errors and they go unhandled. I would like to prevent that and at least get an error msg. – User12547645 Apr 23 '18 at 17:43
  • @User12547645 You can't do that portably. Most errors are undefined behavior and you cannot reliably do anything after they've been encountered. Your best bet is to run a debug build with a debugger. – François Andrieux Apr 23 '18 at 17:44
  • @Carl I agree. Is there a way how to do it without a debugger, thoug debugging can be tedious, which is why I would like to have an overview over the errors done in the program (or at least the first error, since to program will exit after the first error) – User12547645 Apr 23 '18 at 18:32
  • @FrançoisAndrieux :( I was expecting that answer, but hoping for something else... – User12547645 Apr 23 '18 at 18:32

0 Answers0