Catching Uncaught exception Exception: SSL read - php

I'm reading a URL that is finicky sometimes and throws a Uncaught exception Exception: SSL read: error:00000000:lib(0):func(0):reason(0), errno 10054 I tried doing this:
try {
$body = Unirest\Request::get($url);
} catch (Exception $e) {
print $e;
return;
}
but still, the error is stopping my task. I would like to know if I am missing that will just execute the catch function and not stop it all together.

Related

Yii 1.1 with PHP 5.6: how to skip Redis connection error silently

I am reading some extra information from Redis and the desired behaviour is to skip connection error silently, if any:
try {
$r = new Redis();
$r->connect("127.0.0.1", "6379");
} catch (Error $e) {
;
} catch (Throwable $e) {
;
}
If Redis fails, monitoring system will show alert to right people to fix it.
Unfortunatelly the code above still causes Yii to fail and produce HTTP 500:
2018/04/09 12:28:04 [error] [php] Redis::connect(): connect() failed: Connection refused
What am I doing wrong?
You need to catch the Exception thrown...
try {
$r = new Redis();
$r->connect("127.0.0.1", "6379");
} catch (\Exception $e) {
;
}
I think you can catch the very specific exception of Predis\Connection\ConnectionException if you need to.

How to catch specific Exception

I sometimes get in the log PHP Fatal error: Uncaught Exception: Connection reset by peer on socket_read()
How can I catch and ignore only this one exception, re-throwing any other?
My example handles all exceptions. If the exception contains the phrase, it allows you to handle that, otherwise, it rethrows an error message.
try {
// Your Code
} catch (Exception $e) {
if ( ! strpos($e->getMessage(), "Connection reset by peer") === false )
throw $e; // THROW IT, ITS A DIFFERENT ERROR
else
{
// Do Your Handling Code
}
}

Exception not catch by Try/Catch in PHP

i've try this example :
<?php
try {
Not_Exist();
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
http://php.net/manual/en/language.exceptions.php
But the error is not catched :
Fatal error: Call to undefined function Not_Exist() in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\mysite\public_html\exception.php on line 3
Why? I'm usually using Dotnet. Maybe i miss something.
error is not an exception. PHP itself doesn't throw exceptions on syntax errors, such as missing functions. You'd need set_error_handler() to "catch" such things - and even then, a custom error handler can't handle parse errors such as this.
There is no Exception being thrown in your code. You're simply hitting a fatal error. If you're trying to find a way around fatal errors, this post may be of help. How do I catch a PHP Fatal Error
If you're trying to catch Exceptions, something like this:
try {
throw new Exception("Exception Message");
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Because that is a fatal error, not an exception. You're going to want to use PHP's function_exists() method and try something like:
try{
function_exists(Not_Exist);
} catch (Exception $e) {
// error here
}
// run Not_Exist here because we know its a valid function.

fatal error: Uncaught exception 'DOMPDF_Exception' with message 'Box property calculation requires containing block width'

When I try to process url http://www.bbc.co.uk/ dompdf throws error
fatal error: Uncaught exception 'DOMPDF_Exception' with message 'Box property calculation requires containing block width' in www\dompdf\include\block_frame_reflower.cls.php on line 171
It seems some settings or some bug?
DOMPDF_Exception is an extension of the Exception class. I'm not sure what parameters it will spit out, but you can dump the array out to see what is being returned:
try{
}catch(DOMPDF_Exception $e){
echo '<pre>',print_r($e),'</pre>';
}
Also found this on Google Code that might help you narrow down, what the actual issue is: http://code.google.com/p/dompdf/issues/detail?id=244
No, you just have to catch exceptions or set your stuff properly.
try {
//Do your stuff here
} catch (Exception $e){
echo $e->message() ;
}
I believe the correct way to return the message is
try {
//Do your stuff here
} catch (Exception $e){
echo $e->getMessage() ;
}

Disabling Xdebug's dumping of caught exceptions

By default Xdebug will dump any exception regardless of whether it is caught or not:
try {
throw new Exception();
}
catch (Exception $e) {
}
echo 'life goes on';
With XDebug enabled and the default settings this piece of code will actually output something like the following (nicely formatted):
( ! ) Exception: in /test.php on line 3 Call Stack
# Time Memory Function Location 1 0.0003 52596 {main}( ) ../test.php:0
life goes on
Is it possible to disable this behaviour and have it dumping only the uncaught exceptions?
Thanks in advance.
UPDATE: I'm about to conclude that this is a bug, since xdebug.show_exception_trace is disabled by default yet it doesn't behave as expected (using Xdebug v2.0.5 with PHP 5.2.10 on Ubuntu 9.10).
Change the xdebug.show_exception_trace option (note it's not enabled by default).
xdebug.show_exception_trace
Type: integer, Default value: 0
When this setting is set to 1, Xdebug will show a stack trace whenever an exception is raised - even if this exception is actually caught.
If your code is namespaced, the catch block should reference \Exception -- with the backslash -- if there's no backslash then PHP will look for Exception in your current namespace. This usually fails and the uncaught exception is passed to Xdebug.
The following code passes the exception to Xdebug:
namespace foo;
try {
new \PDO(0);
} catch (Exception $e) {
echo "Caught!";
}
// Fatal error: Uncaught exception...
Adding a backslash before Exception will look for (and find) Exception in the global namespace:
namespace foo;
try {
new \PDO(0);
} catch (\Exception $e) {
echo "Caught!";
}
// Exception caught correctly
Manually throwing exceptions can be confusing (which is why I used PDO above). If we try to throw an Exception from the current namespace, PHP tells us Exception doesn't exist there:
namespace foo;
try {
throw new Exception();
} catch (Exception $e) {
echo "Caught!";
}
// Fatal error: Class 'foo\Exception' not found
Throwing a global exception without a global reference in the catch block fails differently:
namespace foo;
try {
throw new \Exception(); // global Exception
} catch (Exception $e) {
echo "Caught!";
}
// Fatal error: Uncaught exception 'Exception' in...
In light of all this, it's probably a good idea to always prefix the catch's Exception with a backslash.
namespace foo;
try {
throw new \Exception();
} catch (\Exception $e) {
echo "Caught!";
}
// Exception caught correctly

Categories