So I recently learned that multiple exceptions can be active at once

I wanted to try it out myself, but I can't find any compiler with a standard library that has std::uncaught_exceptions implemented. This is my test code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <stdexcept>
#include <exception>

struct ResonanceCascade final
{
    ResonanceCascade()
    {
        std::cout << "ResonanceCascade ctor" << std::endl;
    }
    ~ResonanceCascade() noexcept(false)
	{
        std::cout << "~ResonanceCascade() begin" << std::endl;
        std::cout << "std::uncaught_exceptions() == " << std::uncaught_exceptions() << std::endl;
        try
        {
    		if(std::uncaught_exceptions() < 5)
    		{
    			ResonanceCascade rc;
    			throw std::logic_error{"Black Mesa"};
    		}
        }
        catch(...)
        {
            std::cout << "~ResonanceCascade() catch" << std::endl;
        }
        std::cout << "~ResonanceCascade() end" << std::endl;
	}
};

int main()
{
    try
    {
    	ResonanceCascade rc;
    	throw std::logic_error{"Black Mesa"};
    }
    catch(...)
    {
        std::cout << "main catch" << std::endl;
    }
}
It seems to work if you use std::uncaught_exception without the s, but of course you need to implement a counter manually in that case.

Can anyone compile this and paste the output?
Thanks MiiNiPaa, didn't know that existed. So it looks like C++ really does support multiple active exceptions at once - absolutely amazing X)

EDIT: Java does too! https://ideone.com/ktzmqg
Last edited on
> So it looks like C++ really does support multiple active exceptions at once

Yes; and if we want, an active exception can be nested within another active exception.
http://en.cppreference.com/w/cpp/error/nested_exception
There's an example at the bottom of the page.
I saw that, but I don' think nesting exceptions means that both exceptions are active at the same time - I thought a new exception type was created to hold the no-longer-active exception and proxy the new exception. It's still a little fuzzy for me though.
> I don' think nesting exceptions means that both exceptions are active at the same time

Yes, you are right; it is the nested exception that becomes the active exception.
Ah, I misread your post. Thanks!
Topic archived. No new replies allowed.