Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

  1. # exceptions.pl
  2. # [email protected]
  3. #
  4. # Here's a little code I use for exception handling. It's really just
  5. # glorfied eval/die. The way to use use it is when you might otherwise
  6. # exit, use &throw to raise an exception. The first enclosing &catch
  7. # handler looks at the exception and decides whether it can catch this kind
  8. # (catch takes a list of regexps to catch), and if so, it returns the one it
  9. # caught. If it *can't* catch it, then it will reraise the exception
  10. # for someone else to possibly see, or to die otherwise.
  11. #
  12. # I use oddly named variables in order to make darn sure I don't conflict
  13. # with my caller. I also hide in my own package, and eval the code in his.
  14. #
  15. # The EXCEPTION: prefix is so you can tell whether it's a user-raised
  16. # exception or a perl-raised one (eval error).
  17. #
  18. # --tom
  19. #
  20. # examples:
  21. # if (&catch('/$user_input/', 'regexp', 'syntax error') {
  22. # warn "oops try again";
  23. # redo;
  24. # }
  25. #
  26. # if ($error = &catch('&subroutine()')) { # catches anything
  27. #
  28. # &throw('bad input') if /^$/;
  29. sub catch {
  30. package exception;
  31. local($__code__, @__exceptions__) = @_;
  32. local($__package__) = caller;
  33. local($__exception__);
  34. eval "package $__package__; $__code__";
  35. if ($__exception__ = &'thrown) {
  36. for (@__exceptions__) {
  37. return $__exception__ if /$__exception__/;
  38. }
  39. &'throw($__exception__);
  40. }
  41. }
  42. sub throw {
  43. local($exception) = @_;
  44. die "EXCEPTION: $exception\n";
  45. }
  46. sub thrown {
  47. $@ =~ /^(EXCEPTION: )+(.+)/ && $2;
  48. }
  49. 1;