Class zerodivisorexception extends exception {
Public Function _ construct (){
Parent: :__ construct ('divide by 0 exception', 101 );
}
Public Function _ tostring (){
$ MSG = $ this-> getmessage ();
$ Code = $ this-> getcode ();
$ File = $ this-> GetFile ();
$ Line = $ this-> Getline ();
$ Ret = "/n -------------------------------------/N ";
$ Ret. = "An error occurred! /N ";
$ Ret. = "error code: $ code/N ";
$ Ret. = "error message: $ MSG/N ";
$ Ret. = "file: $ File/N ";
$ Ret. = "row number: $ line/N ";
$ Ret. = "-------------------------------------/n ";
Return $ ret;
}
}
Class notexactdivisionexception extends exception {
Private $ _ integer;
Private $ _ fraction;
Public Function _ construct ($ integer, $ fraction ){
Parent: :__ construct ('exception cannot be divisible ', 102 );
$ This-> _ integer = $ integer;
$ This-> _ fraction = $ fraction;
}
Public Function getinteger (){
Return $ this-> _ integer;
}
Public Function getfraction (){
Return $ this-> _ fraction;
}
}
Function Div ($ dividend, $ divisor ){
Try {
Echo "$ dividend divided by $ divisor->/N ";
If (0 = $ divisor ){
Throw new zerodivisorexception ();
}
Else if ($ dividend % $ divisor! = 0 ){
$ Integer = (INT) ($ dividend/$ divisor );
$ Fraction = $ dividend % $ divisor;
Throw new notexactdivisionexception ($ integer, $ fraction );
}
$ Result = $ dividend/$ divisor;
Echo "The result is $ result/n ";
}
Catch (notexactdivisionexception $ e) {# The following cannot be "Result: $ e-> getinteger (), remainder: $ e-> getfraction ()/n ";
Echo "Result:". $ e-> getinteger (). ", the remainder is". $ e-> getfraction (). "/n ";
}
Catch (zerodivisorexception $ e ){
Echo $ E;
}
Catch (exception $ e) {# Put catch blocks that catch exception types at the end to catch any other exceptions.
Echo $ E;
}
}
Div (100, 10 );
Div (100, 0 );
Div (100, 30 );
?>