error_exception_handling - Week 2

2022-08-29

Practical 3: Write code for function called testEvenNumber which outputs the following message when it is invoked


<?php

function testEvenNumber($num) {

// Write your code here
// hint: use try { } catch() { } and finally {} block

}


// Code for testing your function

echo 'Output for ODD Number';  

testEvenNumber(5);         

echo '</br> </br>';  

echo 'Output for EVEN Number';  

testEvenNumber(10);  
?>

Practical 4: Use the set_error_handler(error_function, error_type) function to set the custom error handler to take over the original error handler using fucntion

<?php
set_error_handler('myError');
function myError($errorNum){
  echo('set_error_handler:123 ' . $errorNum .  '');
}
set_error_handler('myError');
trigger_error("Something gone wrong123");

Practical 5: Use the set_error_handler(error_function, error_type) function to set the custom error handler to take over the original error handler using class

<?php
class ErrorClass {
    // Must be a static public method
    public static function myError($errorNum){
      echo('set_error_handler:123 ' . $errorNum . ':' . $errorMs . ' in ' . $errorFile . ' on ' . $errorLine . ' line ');
    }
}
set_exception_handler(['ErrorClass', 'myError']);
throw new Exception('Uncaught Exception occurred---');

?>