If-success error handling

Материал из fidoman.ru
Версия от 19:01, 15 июля 2026; 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...»)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)

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.