exceptions && exception objects

exceptions && exception objects

Post by Steven Sh » Thu, 13 Feb 2003 09:32:43



Hi, I'm looking for tutorials/sample-code/book-recommendations for learning about exceptions, exception objects and their effective use.

Cheers,
Steve.

 
 
 

exceptions && exception objects

Post by wigg.. » Thu, 13 Feb 2003 23:14:01


------------------------------------------------


> Hi, I'm looking for tutorials/sample-code/book-recommendations for learning about exceptions, exception objects and their effective use.

http://www.perl.com/pub/a/2002/11/14/exception.html

http://danconia.org

 
 
 

exceptions && exception objects

Post by Davi » Fri, 14 Feb 2003 03:59:21



> Hi, I'm looking for tutorials/sample-code/book-recommendations for
> learning about exceptions, exception objects and their effective use.

> Cheers,
> Steve.


your error classes carefully, you can mimi (somewhat) Java's approach to
exception handling as well:

!/usr/bin/perl -w
use strict;

#--
#-- here is my top error class which all sub error inherite from
#--
package Error;

use Exporter;


use overload '""' => sub { return "Error: $_[0]->{errst}" };

sub new{
        my $class = shift;
        my $errst = shift;

        return bless {errst => $errst} => $class;

Quote:}

1;

__END__

!/usr/bin/perl -w
use strict;

#--
#-- this is to simulate i/o error which is a child class of error
#--
package IOError;

use Exporter;
use Error;

#--
#-- i/o error is a type of error
#--

use overload '""' => sub { return "IOError: $_[0]->{errst}" };

sub new{
        my $class = shift;
        my $errst = shift;

        return bless {errst => $errst} => $class;

Quote:}

1;

__END__

the following shows how each of those works:

!/usr/bin/perl -w

use Error;
use IOError;

#--
#-- mimi Java's try
#--
eval{
        function(1);

Quote:};

#--
#-- mimi Java's catch with most specific error first
#-- and then all other error. catch the i/o error first
#-- and then just error of any kind. if no error, no error
#-- is printed
#--



Quote:}else{

        print "no error\n";

Quote:}

sub function{
        my $number = shift;
        if($number == 1){
                die new Error("Just an error");
        }elsif($number == 2){
                die new IOError("Just an IOError");
        }

Quote:}

__END__

prints:

Error: Just an error

you can build the class tree as deep as you want and with as many
different types of error as you want.

david

 
 
 

exceptions && exception objects

Post by Steven Sh » Fri, 14 Feb 2003 13:34:52


Quote:> http://www.perl.com/pub/a/2002/11/14/exception.html

Thanks. It was a good article. I really liked 'use Fatal'. I've played around with it a bit. I've attached my test program. I'm wondering how to wrap the close method of an IO:File to be do-or-die style.

Cheers,
Steve.

  fatal.pl
< 1K Download
 
 
 

exceptions && exception objects

Post by R. Joseph Newt » Sat, 15 Feb 2003 12:56:21



> Hi, I'm looking for tutorials/sample-code/book-recommendations for learning about exceptions, exception objects and their effective use.

> Cheers,
> Steve.

Hi Steve,

You may be in the wrong neck of the woods for that purpose.  Although Perl has some techniques for handling errors, I have not heard of anything like the Exception models used in C++ or Java.

Joseph

 
 
 

exceptions && exception objects

Post by Simr » Sat, 15 Feb 2003 13:14:01


If java style exceptions are what is needed, then try:

http://search.cpan.org/author/PJORDAN/Exception-1.4/Exception.pm

http://search.cpan.org/author/IX/libservlet-0.9.2/lib/Servlet/Servlet...

simran.



> > Hi, I'm looking for tutorials/sample-code/book-recommendations for learning about exceptions, exception objects and their effective use.

> > Cheers,
> > Steve.

> Hi Steve,

> You may be in the wrong neck of the woods for that purpose.  Although Perl has some techniques for handling errors, I have not heard of anything like the Exception models used in C++ or Java.

> Joseph