php - How can I trigger errors on webpages so I can take a screen capture for visual display?
I want to be able to "make" browser display errors, like forbidden, 503 etc...
Is there some kind of sandbox where I could do this?
I need the errors to be rendered in google chrome, then take a screen capture of browser window.
Something like image under it, but force the browser to display something like this, maybe in Chrome dev tools could be done?
Answer
Solution:
You can do this with try-catch condition and set_exception_handler function like this:
<?php
class MyCustomException extends Exception { }
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
set_exception_handler('exception_handler');
try {
throw new Exception('Uncaught Exception');
} catch (MyCustomException $e) {
echo "Your custom exception caught ";
echo $e->getMessage();
} finally {
echo "I'm always here";
}
print "Not executed";
For more information you may visit site https://netgen.io/blog/modern-error-handling-in-php
Answer
Solution:
You can simply use try
and catch
, you can check function inside try
and throw error to catch
.
For example, I need to run MySQL if is there any error then report it.
So here is function for it:
try {
if(!mysqli_query($conn,$query)){
throw new Exception(you can put query error)
}
} catch(Exception $e) {
print_r($e)
}
The $e
inside catch will give you error.