Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
One solution would be to catch a more generic base class, like std::exception. If all your exceptions derive from that, you should be safe. Another solution I like better is using destructors to d...
Answer
#6: Post edited
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
- Another solution I like better is using destructors to do clean-up instead of lots of try-catches. The more clean up is done automatically by destructors, the better. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT`, `SCOPE_FAIL` and `SCOPE_SUCCESS` from [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:
- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
The macros create an anonymous object, which execute the block in its destructor either always or conditionally depending on the macro.- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
- Another solution I like better is using destructors to do clean-up instead of lots of try-catches. The more clean up is done automatically by destructors, the better. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT`, `SCOPE_FAIL` and `SCOPE_SUCCESS` from [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:
- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
- The macros create an anonymous object, which execute the block in its destructor either always or conditionally depending on the macro. This is the most natural way I tend to do error-handling. I usually don't care about the particular exception being thrown. All I want to do is to make sure code either succeeds or fails gracefully and cleans up automatically.
- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
#5: Post edited
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
- Another solution I like better is using destructors to do clean-up instead of lots of try-catches. The more clean up is done automatically by destructors, the better. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT`, `SCOPE_FAIL` and `SCOPE_SUCCESS` from [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:
- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
- Another solution I like better is using destructors to do clean-up instead of lots of try-catches. The more clean up is done automatically by destructors, the better. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT`, `SCOPE_FAIL` and `SCOPE_SUCCESS` from [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:
- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
- The macros create an anonymous object, which execute the block in its destructor either always or conditionally depending on the macro.
- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
#4: Post edited
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
Another solution I like better is using destructors to do clean-up instead of lots of try-catches. The more clean up is done automatically by destructors, the better. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT` from [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
- Another solution I like better is using destructors to do clean-up instead of lots of try-catches. The more clean up is done automatically by destructors, the better. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT`, `SCOPE_FAIL` and `SCOPE_SUCCESS` from [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:
- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
#3: Post edited
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
Another solution I like better is using destructors to do clean-up instead of lots of try-catches. The more clean up is done automatically by destructors, the better. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT` in [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
- Another solution I like better is using destructors to do clean-up instead of lots of try-catches. The more clean up is done automatically by destructors, the better. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT` from [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:
- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
#2: Post edited
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
Another solution I like better is using destructors to do clean-up instead of lots of try-catches. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT` in [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
- One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe.
- Another solution I like better is using destructors to do clean-up instead of lots of try-catches. The more clean up is done automatically by destructors, the better. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT` in [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like:
- ```
- file.open();
- SCOPE_EXIT{ file.close(); };
- SCOPE_FAIL{ errorState = true; };
- ```
- I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.
#1: Initial revision
One solution would be to catch a more generic base class, like `std::exception`. If all your exceptions derive from that, you should be safe. Another solution I like better is using destructors to do clean-up instead of lots of try-catches. I recommend you watch this CppCon talk titled [Declarative Control Flow](https://youtu.be/WjTrfoiB0MQ). The video shows a very convenient way of using destructors with macros like `SCOPE_EXIT` in [folly/ScopeGuard.h](https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h). You can basically write declarative code like: ``` file.open(); SCOPE_EXIT{ file.close(); }; SCOPE_FAIL{ errorState = true; }; ``` I'd argue that the destructor of `file` should handle closing, but in case you're working with some legacy API, this could be useful.