1

I am currently having a difficult time to grasp the purpose/use of exceptions in C++.

In other languages that im more familiar with like C# exceptions are automatically thrown if something goes wrong. For example a division by zero or an acess violation because you acessed a not assigned reference. What i used to do in those Languages, is to wrap sections of my code in try/catch blocks where it was possible for something horrible to happen. For example processing data that comes from a file that might be corrupted.

Now while diving a bit deeper into C++ i noticed both divisions by zero and segfaults don't generate exceptions:

#include<iostream>

void produceCrash()
{
    int i = 1/0; // program just crashes no exception

    int* p;
    *p = 10; // same thing here
}

int main()
{
    try
    {
        produceCrash();
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    catch(...)
    {
        std::cerr << "Exception thrown!" << std::endl;
    }   

    return 0;
}

From what i understand, it is only possible to catch exceptions that got explicitly thrown by throw. That would mean any unexpected error simply can not be catched. Does this mean exceptions in C++ are simply an (cleaner and more readable) alternative for stuff like error code returning and just a way to control your program flow?

Eric
  • 1,183
  • 6
  • 17
  • 1
    `[...]For example a division by zero or an acess violation because you acessed a not assigned reference. [...] From what i understand, it is only possible to catch exceptions that got explicitly thrown by throw. [...]` - C# does the extra bit for you, by checking this things. C++ is more lowlevel. The general idea behind exceptions is in both languages the same (more or less). – user1810087 Jan 05 '20 at 13:10
  • You are right, exceptions are a flow control structure. Not every operation is checked whether it should raise an exception, large parts are left as programming error ("undefined behaviour"), in particular those that could have been prevented. – Ulrich Eckhardt Jan 05 '20 at 13:13

1 Answers1

3

Unfortunately there are two kinds of exceptions, that are easily confused by beginners:

The first is the language based exceptions, like you have with throw, try, catch and std::exception in C++.

The other kind are hardware exceptions, like what happens if you divide by zero or attempt to access a memory page not owned by your process.

C++ exceptions and hardware exceptions are separate and you can't use C++ exceptions to catch hardware exceptions. As you understand, you can only catch exceptions that are explicitly created by throw.

Many operating systems have ways to catch hardware exceptions though, for example signals could be used on POSIX systems (like for example SIGSEGV when attempting unallocated page access and similar). This makes it mostly operating-system dependent. But also note that many hardware exceptions are not possible to recover from, you can catch them but not really continue in any meaningful way anyway.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • for example, division by zero brought up by the OP is a hardware "exception" :) https://stackoverflow.com/questions/21852270/number-divide-by-zero-is-hardware-exception – maestromusica Jan 05 '20 at 13:31
  • 1
    There’s a third flavor of exceptions, too: floating-point exceptions, which are the result of **valid** fp operations; they are not C++ exceptions and they are not hardware exceptions. – Pete Becker Jan 05 '20 at 19:26