home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 5014 < prev    next >
Encoding:
Text File  |  1992-07-29  |  1.9 KB  |  64 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!fmrco!fmrco!asherman
  3. From: asherman@laser.fmrco.com (Aaron Sherman)
  4. Subject: Exception handling (was: Re: or puzzlement)
  5. In-Reply-To: flee@cs.psu.edu's message of Mon, 27 Jul 1992 22:42:24 GMT
  6. Message-ID: <ASHERMAN.92Jul29102950@laser.fmrco.com>
  7. Sender: news@fmrco.uucp
  8. Reply-To: asherman@fmrco.COM
  9. Organization: I-Kinetics, 19 Bishop-Allen Dr., Cambridge, MA
  10. References: <BrtMFu.D0L@unx.sas.com> <1992Jul23.191243.9731@netlabs.com>
  11.     <Bs2HMz.Gqp@cs.psu.edu>
  12. Date: Wed, 29 Jul 1992 15:29:50 GMT
  13. Lines: 49
  14.  
  15.  
  16. One thing that I was doing, when I was trying to write a perl
  17. interface to the X protocol, involved an exception handling library 
  18. that would handle the concept of having multiple errors occur in one
  19. call.
  20.  
  21. What I came up with was something that looked like:
  22.  
  23.     &lib_error_procname($0);
  24.     
  25.     unless (&openfoo)
  26.     {
  27.         &lib_error_die('opening output file');
  28.     }
  29.  
  30.     sub openfoo
  31.     {
  32.         # This function will return false if BOTH opens fail.
  33.         unless(open(FOO,"foo"))
  34.         {
  35.             # Log that the open failed, but do not return false.
  36.             &lib_error('OPENFAIL',"foo: $!");
  37.             # If this open fails, then the routine fails.
  38.             open(FOO,'bar') || &lib_error('OPENFAIL',"bar: $!");
  39.         }
  40.     }
  41.  
  42. If this failed, then you would get:
  43.  
  44.     testprog: Error opening output file:
  45.  
  46.         Open call failed: foo: No such file or directory.
  47.         Open call failed: bar: No such file or directory.
  48.  
  49. This has many personal quirks (like wanting a "list of known errors"
  50. ala perror), but served the purpose of having subroutines that didn't
  51. need to know why they were being called, or print out errors, and
  52. still returned a list of problems.
  53.  
  54. BTW: one important feature is that &lib_error ALWAYS returns false.
  55.  
  56.  
  57.             -AJS
  58.  
  59. --
  60. --------
  61. Disclaimer: I am solely responsible for the content of this message.
  62. The views expressed here may not be the views of I-Kinetics, Fidelity,
  63. any of the Fidelity-owned corporations or my mother.
  64.