0% found this document useful (0 votes)
6 views2 pages

EXCEPTION

The PHP script defines a custom exception class 'Aime' that extends the built-in Exception class. It attempts to perform a division operation where the denominator is zero, which triggers a DivisionByZeroError, caught and rethrown as an Aime exception with a custom message. Finally, the script catches the Aime exception and outputs the corresponding error message.

Uploaded by

ishimwelukaku08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

EXCEPTION

The PHP script defines a custom exception class 'Aime' that extends the built-in Exception class. It attempts to perform a division operation where the denominator is zero, which triggers a DivisionByZeroError, caught and rethrown as an Aime exception with a custom message. Finally, the script catches the Aime exception and outputs the corresponding error message.

Uploaded by

ishimwelukaku08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

explain every block of code in the following codes:

<?php class Aime extends Exception { public function __construct($message)


{ parent::__construct($message); } } try { try { $numerator = 10; $denominator = 0;
if ($denominator == 0) { throw new DivisionByZeroError(); } $result = $numerator /
$denominator; } catch (DivisionByZeroError $e) { throw new Aime("You tried to
divide by zero!"); } } catch (Aime $e) { echo "Caught an exception: " . $e-
>getMessage(); } ?>

let’s break this down piece by piece:

<?php
class Aime extends Exception {
public function __construct($message) {
parent::__construct($message);
}
}
<?php: This indicates the beginning of a PHP script.

class Aime extends Exception {}: This defines a custom exception class named Aime
that extends the built-in Exception class.

public function __construct($message) {}: This is the constructor method for the
Aime class, which takes a $message parameter.

parent::__construct($message);: This calls the parent (Exception) class's


constructor, passing the $message.

try {
try {
$numerator = 10;
$denominator = 0;

if ($denominator == 0) {
throw new DivisionByZeroError();
}

$result = $numerator / $denominator;


} catch (DivisionByZeroError $e) {
throw new Aime("You tried to divide by zero!");
}
} catch (Aime $e) {
echo "Caught an exception: " . $e->getMessage();
}
?>
try { ... }: This block tries to execute the code within it, and any exceptions
thrown will be caught by the catch blocks.

$numerator = 10; $denominator = 0;: These lines define and initialize the variables
numerator and denominator.

if ($denominator == 0) { throw new DivisionByZeroError(); }: This checks if the


denominator is zero and, if it is, throws a DivisionByZeroError.

$result = $numerator / $denominator;: This would normally execute the division


operation, but it's skipped if the denominator is zero.
catch (DivisionByZeroError $e) { throw new Aime("You tried to divide by zero!"); }:
This catches a DivisionByZeroError and then throws a custom Aime exception with a
specific message.

catch (Aime $e) { echo "Caught an exception: " . $e->getMessage(); }: This catches
the Aime exception and prints the custom error message.

In essence, this script attempts a division by zero, catches the standard error for
this, converts it to a custom error message, and then catches and displays that
custom error message.

You might also like