User Tools

Site Tools


kurs:oo-error_classes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

kurs:oo-error_classes [2014/09/10 21:22] (current)
Line 1: Line 1:
 +<​code>​
 +## CatchDivByZero.pl
 +use strict;
 +use warnings;
 +
 +use Error qw(:try);
 +use Arithmetic;
 +use DivByZero;
 +
 +my $a = 10;
 +my $b = 0;
 +my $result;
 +
 +try {
 +  $result = Arithmetic::​divide($a,​$b);​
 +} catch DivByZero with {   ## catch it here and not in the Class Arithmetic.pm ​
 +  $b = 1;                  ## so we can still do something else and not just die there
 +  $result = Arithmetic::​divide($a,​$b);​
 +} catch Error with {
 +  my $e = shift;
 +  print "​Unhandled Exception $e\n";
 +};
 +
 +printf "The result is %d\n", $result;
 +</​code>​
 +
 +
 +<​code>​
 +## Arithmetic.pm
 +package Arithmetic;
 +
 +use strict;
 +use warnings;
 +use Error qw(:try);
 +use DivByZero;
 +
 +sub divide {
 +  my ($a, $b)  = @_;
 +  my $r;
 +  throw DivByZero if $b == 0;
 +  $r = $a/$b;
 +  return $r;
 +}
 +
 +1;
 +</​code>​
 +
 +<​code>​
 +## DivByZero.pm
 +package DivByZero;
 +
 +use Moose;
 +extends '​Error';​
 +
 +1;
 +</​code>​
  
kurs/oo-error_classes.txt ยท Last modified: 2014/09/10 21:22 (external edit)