Catching ServiceManager Exceptions - php

When I use the service manager to get an object which throws an exception during its initialization, I'm unable to catch this one with a simple :
try {
$hybridAuth = $this->getServiceManager()->get('HybridAuth');
}
catch (Exception $e) {
var_dump('catched');
}
Instead the Exception is catched by Zend with a famous "An error occurred" page
Which shows me a "snowball" of thrown Exceptions (because a service could not be initialized and others are depending on, etc)
How can I catch the exception (and then put some code logic) to prevent Zend from catching it ?

I think you problem isn't on how to catch ServiceManager Exceptions.
The way you're doing it is wrong :
catch (Exception $e)
Will catch exception from the current namespace. What You need is to catch PHP global exception.
You have the same issue as this one.
Change your code by this one :
try {
$hybridAuth = $this->getServiceManager()->get('HybridAuth');
}
catch (\Exception $e) {
var_dump('catched');
}
Or import the global php exception by adding this :
use \Exception;

Related

How to find out what the exact Throwable class is?

I have a try/catch block where I catch all Throwable exceptions.
try {
...
} catch (Throwable $ex) {
...
}
How do I, at runtime, figure out what the exact class of the throw exception is? I want to add multiple catch blocks to handle different exceptions differently, but am unable to find out the types of exceptions that are thrown.
Try to dump get_class($ex) inside your catch block. It will give you the class name of $ex.
After the class name is found, you can use catch with exact class exception.

Having issue with extended exceptions using Yii 2

I'm using the Yii 2 framework and it uses a number of extended exceptions and I'm having an issue with them where I threw a UserException but it ended up being caught by the base Exception but I'm not sure why!?
The code:
try {
//........
if ($reader->count() > 0) {
if (!$already_active) {
//.....
} else {
throw new UserException('You have already activated your account; you may continue to login.');
}
}
} catch (\Exception $e) {
// User exception above is caught in here?
} catch (UserException $e) {
// Rethrow the exception
throw $e;
}
Shouldn't the User Exception be passed onto and caught by the second catch?
From http://php.net/manual/en/language.exceptions.php
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block.
The catch block for Exception will be executed since Exception is a parent of UserException and therefore any object of type UserException is also of type Exception.
Therefore you should refactor your code to have the catch block for child classes first. In your case UserException should be first.
If you take a look at UserException class you can see:
class UserException extends Exception
Therefore, Exception has the higher priority.
However you can do just:
//if something's wrong
throw new \yii\base\UserException('You have already activated your account; you may continue to login.');

Why does `catch (Exception $e)` not handle this `ErrorException`?

I get the ErrorException on the function call bellow. How can this be? Why is it not caught?
try {
static::$function_name($url);
}
catch (Exception $e) {}
The underlying reason for the error is a file_put_contents call. I'm using the Laravel 4 framework, if it makes any difference.
I suspect that you need to write this:
try {
static::$function_name($url);
} catch (\Exception $e) {}
Note the \ in front of Exception.
When you have declared a namespace, you need to specify the root namespace in front of classes like Exception, otherwise the catch block here will be looking for \Your\Namespace\Exception, and not just \Exception

PHP, Exceptions

I am Studying about Exceptions At W3schools At this Link, inside the link just before the heading:
"Re-throwing Exceptions"
there is a sentence that say:
"If the exception thrown were of the class customException and there
were no customException catch, only the base exception catch, the
exception would be handled there."
If can someone please give me an example for this sentence i will be very thankful.
Basically they're saying if you don't have a catch statement set up to catch customException it will fall through to the general Exception catch statement.
In this example, the first catch statement will catch customException because it explicitly is designed to do so.
try {
// fail
throw new customException();
}
catch (customException $e) {
// catch custom exception
}
catch (Exception $e) {
// catch any uncaught exceptions
}
In the next example, because that clause it missing the generic Exception catch block will catch it instead:
try {
// fail
throw new customException();
}
catch (Exception $e) {
// catch any uncaught exceptions
}
Take a look at Example #2 on this page of the PHP manual
And w3schools isn't the best place to learn PHP, it's (in)famous for its errors
Say you have a custom exception defined as
class CustomException extends Exception.
Now somewhere in your code:
try
{
// do something that throws a CustomException
}
catch(Exception $e) // you are only catching Exception not CustomException,
// because there isn't a specific catch block for CustomException, and
// because Exception is a supertype of CustomException, the exception will
// still be caught here.
{
echo 'Message: ' .$e->getMessage();
}
So what it means is that because CustomException is a subclass of Exception, as long as you catch the Exception type of the superclass without a catch block for the more specific sub class type, it will still be caught
There are two catch blocks in the example. The first catches only customException; the next one catches any Exception. If the first block catches anything, you never get to the second block. But since a customException is also an example of an Exception, it would have gotten caught there if we didn't have the first catch block. This way, the first catch catches it, and the execution never reaches the second.
In the meantime, read the link I posted above, and why w3schools is a bad idea.

What is the point of using custom exception class in php?

In some libraries it is common practice to make custom Exception classes for every error condition, like:
class FileNotFound_Exception extends Exception {}
You can handle certain type of Exception, however you cannot read all source code of all libraries to remember each Exception class, and cannot take full advantage of using custom Exceptions. Most of time I just catching them with base Exception class:
catch (Exception $e)
{
// log and display friendly error
}
Is there other ways to have benefit of custom Exception classes, without writing long list of catch blocks?
I like Exceptions, but don't know how to use them properly. Thank you.
The benefit of having your own Exception class is that you, as the author of the library, can catch it and handle it.
try {
if(somethingBadHappens) {
throw MyCustomException('msg',0)
}
} catch (MyCustomException $e) {
if(IcanHandleIt) {
handleMyCustomException($e);
} else {
//InvalidArgumentException is used here as an example of 'common' exception
throw new InvalidArgumentException('I couldnt handle this!',1,$e);
}
}
Well, custom exception classes lets you route your errors properly for better handling.
if you have a class
class Known_Exception extends Exception {}
and a try catch block like this:
try {
// something known to break
} catch (Known_Exception $e) {
// handle known exception
} catch (Exception $e) {
// Handle unknown exception
}
Then you know that Exception $e is an unknown error situation and can handle that accordingly, and that is pretty useful to me.

Categories