aesophor / wmderland

🌳 X11 tiling window manager using space partitioning trees
https://www.reddit.com/r/unixporn/comments/fb8ve1/wmderland_104_time_to_move_on/
MIT License
409 stars 15 forks source link

Catch exceptions in main()? #13

Closed elfring closed 5 years ago

elfring commented 5 years ago

I expect that exception handling is usually supported by a C++ program. I wonder why your function “main” does not contain corresponding try and catch instructions so far.

How do you think about recommendations by Matthew Wilson in an article?

Would you like to adjust the implementation if you consider effects for uncaught/unhandled exceptions?

aesophor commented 5 years ago

Thank you! I'm fairly new to C++ and here's my opinion on this, so if I'm wrong please kindly give me advice!

I followed Google C++ Style Guide and saw they do not use C++ exceptions, also:

elfring commented 5 years ago

How many details do you find relevant from the three information sources (which I referenced)?

aesophor commented 5 years ago

I've been reading Matthew Wilson's article, but I don't dare to say that I understand it completely. I did say that there won't be any thrown exceptions before but now I think I was totally wrong, since I use lots of stuff from STL (for example, std::vector) which can throw std::out_of_index.

I've added a try-catch block in main.cpp which catches const std::exception& ex. I think this will at least ensure the destructor of WindowManager is called, instead of simply relying on the OS to clean up the mess for us. I've also read that this ensures named pipes/mutex to be cleaned up should the program crashes.

In Matthew Wilson's article, he mentioned that catch (...) is not desirable, since it could catch access violations, divide-by-zero and quench them, (I think this makes it harder to debug), so I decide to catch std::exception only.

elfring commented 5 years ago

Will any aspects get more of your software development attention for a “production-quality main()”?

aesophor commented 5 years ago

Hi there! I've added catch (const std::bad_alloc&) in ffc0478 (as the first exception to catch). Also added catch (...) to avoid uncaught exceptions which ensures stack unwinding occurs.

elfring commented 5 years ago

Thanks for another small source code adjustment.