Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

77%
+5 −0
Q&A Multiple catches with almost the same code.

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...

posted 3y ago by Ayxan Haqverdili‭  ·  edited 3y ago by Ayxan Haqverdili‭

Answer
#6: Post edited by user avatar Ayxan Haqverdili‭ · 2021-03-09T13:33:40Z (about 3 years ago)
  • 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 by user avatar Ayxan Haqverdili‭ · 2021-03-09T13:30:46Z (about 3 years ago)
  • 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 by user avatar Ayxan Haqverdili‭ · 2021-03-09T09:38:13Z (about 3 years ago)
  • 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 by user avatar Ayxan Haqverdili‭ · 2021-03-09T09:36:12Z (about 3 years ago)
  • 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 by user avatar Ayxan Haqverdili‭ · 2021-03-09T09:35:17Z (about 3 years ago)
  • 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 by user avatar Ayxan Haqverdili‭ · 2021-03-09T09:34:18Z (about 3 years ago)
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.