Advertisement
happy-barney

try-catch and finally

Feb 15th, 2021
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. -- a) FINALLY not called in case of exception
  2. -- its fine with one variable though it separates variable declaration, its initialization, and its finally block
  3.  
  4. my $foo;
  5. try {
  6. $foo = init ...;
  7. }
  8. catch {
  9. die ..;
  10. }
  11.  
  12. FINALLY { $foo->destroy }
  13.  
  14. -- b) FINALLY not called in case return
  15. -- to solve this finally must be specified before try block (therefor with check)
  16.  
  17. my $foo;
  18. FINALLY { $foo->destroy if $foo }
  19.  
  20. try {
  21. $foo = init ...;
  22. return if ...;
  23. }
  24. catch {
  25. die ..;
  26. }
  27.  
  28. -- c) two coupled variables
  29.  
  30. my $foo;
  31. FINALLY { $foo->destroy if $foo }
  32. my $bar;
  33. FINALLY { $foo->unregistry ($bar) and $bar->destroy if $bar && $foo }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement