2 ms·
AFAIK Python/C# use a similar approach to Go - instead of `defer`, they have `using`/`with` statements. Go's `defer` seems more flexible though - it can execute
by pansa2 2y ago
AFAIK Python/C# use a similar approach to Go - instead of `defer`, they have `using`/`with` statements. Go's `defer` seems more flexible though - it can execute custom code each time it's used, whereas `using`/`with` always call `__exit__`/`Dispose`.
How does the Python/C# approach compare to Go's in this situation? How are errors in `__exit__`/`Dispose` handled?
- d0mine 2y ago> Exceptions that occur during execution of this method will replace any exception that occurred in the body of the with statement https://docs.python.org/3/library/stdtypes.html#contextmanager.__exit__ https://docs.python.org/3/library/stdtypes.html#contextmanag...
- pansa2 2y agoThanks!
- Too 2y agoFrom the application point of view, exceptions are more natural to catch, so if the close inside the exit-function throws, the nearest catch block will not be far up the stack. Compared to go, where you are much more unlikely to have a recover block, because it is considered such a rare case.