I have code like this
try {
header("Location: http://www.google.com\n-abc");
}
catch (Exception $e) {
error_log(print_r($_POST, true));
error_log(print_r($_GET, true));
error_log(print_r($_SERVER, true));
}
Without the try {} catch {} block, I can see the POST, GET and SERVER variables in my error_log, but with the try {} catch {} block, I only see the default PHP error.
Is there a way to show the POST, GET, and SERVER variables in a try {} catch {} block?
Is there a way to have PHP include POST, GET, and SERVER variables for ALL errors that get logged to file and not just wherever I have added error_log(print_r($_POST, true)); ....?
try/catch is for when you want to throw an exception to prevent php fatal errors from killing the page or to make debugging easier, generally speaking. This is how you would use a try/catch:
try {
if($a === true)
{
header("Location: http://www.google.com\n-abc");
}
else
{
throw new Exception('$a was not true');
}
}
catch (Exception $e) {
error_log(print_r($_POST, true));
error_log(print_r($_GET, true));
error_log(print_r($_SERVER, true));
echo $e->getMessage(); // $a was not true
}
The reason you don't see your error logs in the catch block in your example is because you never threw an exception so PHP will never look inside that catch block to log your variables.
If you simply want to get your example to work you'd just throw an exception to get PHP in that catch block:
try
{
throw new Exception('redirecting...');
}
catch (Exception $e)
{
error_log(print_r($_POST, true));
error_log(print_r($_GET, true));
error_log(print_r($_SERVER, true));
header("Location: http://www.google.com\n-abc");
}
That's just silly though :)
But default PHP functions like header will not throw exceptions.
If you want them to throw exceptions you will need to set the error handler to use ErrorException:
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");
I think you have a few questions/issues here.
First, a try/catch block is used for Exceptions. The catch {} code is only executed if a Exception of the type specified is thrown.
Second, the header() function does not throw exceptions. Instead it just generates a PHP error directly.
Thirdly, to log those variables for all errors take a look at set_error_handler(), which should also take care of the above, second issue.
try/catch would only work if you're using an object that actually throws exception. header() is NOT an object and will NEVER throw an exception. Like I said in my answer to your previous version of this question, you need to set up a custom error handler.
That's how try/catch is supposed to work: the catch block executes when an exception is thrown in the try block (and only in that case).
The header() function may generate a warning if headers are already sent, but it will never throw an exception.
Answering your questions:
Your catch block is just fine but you need code that can actually throw exceptions (typically object-oriented code, but not necessarily). However, you can configure PHP to convert regular errors into exceptions if you use set_error_handler() to create a custom error handler and use throw inside of it.
Same as above: set_error_handler() and set_exception_handler().
Sounds like you want to have some extra information output to your log when an error is thrown. You should check out custom error handling in PHP. You can define specific ways to handle errors in your scripts and add functionality that is not there by default.PHP Set error handler
Related
try {
} catch (\Exception $ex) {
}
In my sample use case, I don't need any information from $ex when \Exception is catched. I just need to do some stuff when \Exception is catched. I need nothing from $ex.
Now ommiting $ex causes syntax error, leaving it usued makes my IDE (Netbeans) throw a warning.
Any way to omit $ex other than $ex = '';?
You can't omit the hinted argument \Exception $ex from the catch statement, because the block needs to know what type of Exception it's there to deal with, even if your code doesn't care.
You don't, however, need to refer to it at all in the catch block. If the NetBeans warning hint about the unused variable is really bothering you, you can always silence all hints of that class by going to
Tools -> Options
"Editor" tab
"Hints" sub-tab
Language: PHP
Uncheck "Unused Variables" in list
(Of course then you won't see warnings if you have other unused variables, but if you just want the hints not to be Warnings, you can always change the "Show as:" setting to "info" instead.)
In php there is way of handling error. Because of that catch expect to have store exception. So you have to provide $ex. But you can leave empty inside catch in your case.
To remember this is the syntax.
try {
// run your code here
}
catch (exception $e) {
//code to handle the exception
}
finally {
//optional code that always runs
}
I want to know what errors can be thrown that I am not catching, for dynamic code - not static code. For example, my code may run without throwing any Exceptions for 10 years and then throw UncaughtException
I want to specifically (non-generically) catch every type of Exception that can be thrown by the methods I am using. How can I know what Exceptions MAY be thrown by these methods?
I want to non-generically catch every type of error that can possibly be thrown for a section of PHP code.
Examples of exceptions that may be thrown:
PDOException
ExpiredException
Now I have this around everything:
try{
...
}catch(Exception $e){
...
}
I'd like to replace it with something like this:
try{
...
}catch(PDOException $e){
...
}catch(ExpiredException $e){
...
}catch(Exception $e){
...
}
I'd like to be confident that I am catching all different kinds of Exception that can be thrown by the methods in the section of code
And if I catch all Exceptions individually, will it be safe to remove this part?:
catch(Exception $e){
...
}
Or are there methods which will simply throw Exception?
My solution preference list (1 is the most-preferred solution):
1: A flag I can turn on that will cause php.exe to warn me about each and every possible type of Exception that is not specifically being caught
2: A way to individually check each method and see what errors can be thrown. Is the documentation the only way to check? or is there some IDE or PHP block that will tell me which Exceptions may be thrown by individual methods?
You can set callback function by using register_shutdown_function() which will call on every end of your php code execution. In this callback function you can check whether any error occurs or not using error_get_last().
For Example:
// Register shutdown function
register_shutdown_function("shutdownTracker");
// Define all error types you want to catch and handle
define('E_FATAL', E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR);
function shutdownTracker() {
$error = error_get_last(); // This will return empty if no error occurs while executing php code.
if(!empty($error) && ($error['type'] & E_FATAL)) {
// Write your code here to handle you error
}
}
Note: You should include this code on top of your code.
Well if you are talking exceptions, you already have the answer
try{
...
}catch(PDOException $e){
...
}catch(ExpiredException $e){
...
}catch(Exception $e){
echo get_class($e); // get's the class of unrecorded exceptions.
//catches any exceptions missed by the above
}
Now if you are talking about "errors" you can do is use a custom error handler
if(!function_exists('myErrorHandler')){
function myErrorHandler($severity, $message, $file = 'UNKNOWN', $line = 'UNKNOWN')
{
throw new ErrorException(
$message,
1,
$severity,
$file,
$line
);
}
set_error_handler('myErrorHandler');
}
What it does is convert all PHP errors to exceptions or rather to ErrorExceptoins.
Now you can go a step further and use these other two functions.
register_shutdown_function
AND
set_exception_handler
I'm actually working on porting what I use to it's own stand alone github project. Right after I finish my eJinn project, which you may be interested in. eJinn is designed to build exception classes based off a config file, so you can have one error per exception file and unique error codes in a project.
You can catch all exceptions without even call try.
function hello($e)
{
if ($e instanceof PDOException){
echo "something".
}else echo $e->getMessage();
}
set_exception_handler('hello');
this should catch all exceptions.
UPDATE 2
I've edited the code so you know what exceptions will be thrown using get_class();
class mycustomException extends Exception{} // making new exception
function hello($e)
{
if ($e instanceof PDOException){ // exception already known
echo "something";
}else{
echo get_class($e); // get exception name.
}
}
set_exception_handler('hello');
throw new mycustomException(); // throw exception that we made.
I was just wondering something. In index.php, I am currently doing something like this
function performFtpOperation() {
global $config;
try {
$ftp = new FTP\FtpClient();
$ftp->connect($config::FTP_SERVER);
$ftp->login($config::FTP_USER, $config::FTP_PASSWORD);
} catch (Exception $e) {
echo 'Error: ', $e->getMessage();
}
}
What I was wondering is if that try catch block is needed? Reason I question it is because my FTP class throws errors if something goes wrong. For instance this is the connect function
public function connect($host, $ssl = false, $port = 21, $timeout = 90)
{
if ($ssl) {
$this->conn = #$this->ftp->ssl_connect($host, $port, $timeout);
} else {
$this->conn = #$this->ftp->connect($host, $port, $timeout);
}
if (!$this->conn) {
throw new Exception('Unable to connect');
}
return $this;
}
So would a try/catch be needed if errors are handled within the class?
Thanks
When an exception is thrown, the code following it will not be executed, and PHP will try to find the matching "catch" block.
If an exception is not caught, a fatal error will be issued with an "Uncaught Exception" message.
Proper exception code should include:
Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown"
Throw - This is how you trigger an exception. Each "throw" must have at least one "catch"
Catch - A "catch" block retrieves an exception and creates an object containing the exception information
The connect class throws exceptions that you need to catch and handle somewhere in your code. It is up to you where to handle it depending on your application design and requirements.
If you decided that you wanted to handle them in the performFtpOperation function, then your use of try...catch there is correct.
If you don't handle them in the performFtpOperation function, then they will bubble up to the code that calls the performFtpOperation function and you can catch & handle them there if you like using try...catch similar to how you did it here. Just remember that you need to catch them somewhere.
Well since you are throwing an exception from within your FTP code, then Yes.
You need to try catch if you work with exeptions, otherwise you generate fatal errors and your script/site will stop work at this point, so try/catch is a part of the exeption handling.
you can inform yourself about this in the php manual
http://php.net/manual/en/language.exceptions.php
I have a try-catch block that iterates over a set of records like this:
try {
foreach ( $json['location'] as $records ) {
$location = DnaExtractionTable::getInstance()->find($records['id']);
$location->setName($records['name']);
$location->setLatitude($records['latitude']);
$location->setLongitude($records['longitude']);
$location->setCountryId($records['country_id']);
$location->setRegionId($records['region_id']);
$location->setIslandId($records['island_id']);
$location->setRemarks($records['remarks']);
$location->save();
}
}
catch (Exception $e) {
...
}
I can catch every exception that is thrown and continue without problems. But I am also trying to "catch" the errors, e.g. when a index does not exist in the $records array.
Is it possible to do that? How I can do it? I've been playing with set_X_handler functions without success.
UPDATE 1:
Following advices from comments and answers, I decided to implement a global error function:
function exceptions_error_handler($severity, $message, $filename, $lineno) {
if (error_reporting() == 0) {
return;
}
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
}
set_error_handler('exceptions_error_handler');
But even if I try to force an error the code does not execute. Since I am developing with Symfony, is there a place to declare that function? Could be Symfony disabling or affecting the set_error_handler function?
UPDATE 2:
Symfony is definitely messing around with my error and exception handlers.
Turning on the debugging mode seemed to activate a Symfony custom exception handler that overrides error reporting.
Turning off the debugging mode seemed to bypass certain exceptions although my try-catch block is configured to catch general Exception objects. Really strange behavior.
Thanks!
See the answer to Handling errors as exceptions. Best methods? for a way to throw exceptions when an error is raised.
I'm trying to catch as many errors as possible in PHP and properly handle them in a non-default way. My question is best illustrated in this example:
<?php
function errorHandler($errno, $errstr, $errfile, $errline){
echo "Error handler here <br>\n";
//throw new Exception($errstr);
}
function shutdownFunction() {
echo "Hi I'm in here <br>\n";
}
set_error_handler("errorHandler");
register_shutdown_function("shutdownFunction");
try {
$undefined->ok(); // causes some error
} catch(Exception $e) {
echo "Caught the exception <br>\n";
}
The result of running this code as a PHP program will indicate that errorHandler() is run, a PHP error is printed (if "display_errors" is set to "On"), and then shutdownFunction() is run.
The problem I'm having arises if I uncomment out that exception throw; I want to throw exceptions on PHP errors as often as possible. If I uncomment the throw statement out, then the error handler is called, which throws an exception thus causing a further error which results in shutdownFunction() not to be called.
It is my understanding that I can't make this error into a catchable exception; however, I would at least like to ensure that the shutdown function is called without restricting my ability to catch at least some php errors as exceptions (meaning I don't want to remove that throw statement in errorHandler()).
Is there some check in the errorHandler that I can do to check whether or not throwing it will cause shutdownFunction() to be bypassed?
Throw an exception from the error handler, then use set_exception_handler to handle uncaught exceptions.
So the way I'm solving this is to check if the $errstr parameter starts with "Undefined variable" - and if it does, then I'm not throwing the exception. I suppose if there are any other errors that have a similar issue, i'll do the same thing with those. I can't find a comprehensive list of php errors and what their affects are though, so it'll have to be ad hoc