If-success error handling: различия между версиями
Материал из fidoman.ru
Sergey (обсуждение | вклад) (Новая страница: «This method will require deeply nested code but makes error handling straight-forward <code> if open file 1 [as f1]: if open file 2 [as f2]: if alloc 1M me...») |
Sergey (обсуждение | вклад) |
||
| Строка 1: | Строка 1: | ||
This method will require deeply nested code but makes error handling straight-forward | This method will require deeply nested code but makes error handling straight-forward | ||
| − | + | if open file 1 [as f1]: | |
| − | if open file 1 [as f1]: | + | if open file 2 [as f2]: |
| − | + | if alloc 1M memory [as p]: | |
| − | + | do some actions | |
| − | + | free memory p | |
| − | + | else: | |
| + | print "memory alloc 1M for the job failed!" | ||
| + | close file 2 | ||
else: | else: | ||
| − | print " | + | print "error open file 2!" |
| − | close file | + | close file 1 |
else: | else: | ||
| − | + | print "error open file 1!" | |
| − | |||
| − | |||
| − | |||
| − | |||
If it would possible to merge constructor/dectructor code into single function this could make initialization/destruction of complex objects more clean. It will require to make special pseudo-function "object initialization" and call it on completeness of construction. Failed constructior then will pass to correct point in destructor to backward deinitialize since last successful point. | If it would possible to merge constructor/dectructor code into single function this could make initialization/destruction of complex objects more clean. It will require to make special pseudo-function "object initialization" and call it on completeness of construction. Failed constructior then will pass to correct point in destructor to backward deinitialize since last successful point. | ||
Текущая версия от 19:02, 15 июля 2026
This method will require deeply nested code but makes error handling straight-forward
if open file 1 [as f1]:
if open file 2 [as f2]:
if alloc 1M memory [as p]:
do some actions
free memory p
else:
print "memory alloc 1M for the job failed!"
close file 2
else:
print "error open file 2!"
close file 1
else:
print "error open file 1!"
If it would possible to merge constructor/dectructor code into single function this could make initialization/destruction of complex objects more clean. It will require to make special pseudo-function "object initialization" and call it on completeness of construction. Failed constructior then will pass to correct point in destructor to backward deinitialize since last successful point.