Hello, the topic about getting an intentional error is not so often you look up to but sometimes you need it to make sure the error is handled correctly. Let’s dive into the content directly.

1
trigger_error("Error Triggered!", E_USER_ERROR);

DOC Reference

1
2
3
4
5
6
7
<?php 

try {
  trigger_error("Error Triggered!", E_USER_ERROR);
} catch(Exception $e) {
  echo 'Message: ' .$e->getMessage();
}

Above code will give you an triggered fatal error.

Intentional Error

Adding below lines to your code will help you display all the errors and warnings.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php 

ini_set ('display_errors', 1);
ini_set ('display_startup_errors', 1);
error_reporting (E_ALL);

try {
  trigger_error("Error Triggered!", E_USER_ERROR);
} catch(Exception $e) {
  echo 'Message: ' .$e->getMessage();
}

Intentional Error

This is how you get an intentional error. Happy Coding 😍