Having issue with extended exceptions using Yii 2 - php

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.');

Related

How to catch an Exception that is chain called into multiple Classes PHP

Suppose I have a repository class, a service class, and finally a controller class.
Now, I have a repository class that uses PDO, and I enclosed the execution into a try-catch block, example, below:
MyRepository {
public function someRepositoryFunction()
{
try {
...some pdo execution
} catch (PDOException $e) {
throw new PDOException($e->getMessage());
}
}
}
My question is, when I call that someRepositoryFunction() in the Service class, do I have to enclose it in a try-catch block also? example, below:
MyService {
public function someServiceFunction()
{
$repo = new MyRepository();
try {
$repo->someRepositoryFunction();
} catch (PDOException $e) {
throw new PDOException($e->getMessage());
}
}
}
and finally, when I call the Service class in the Controller, do I also have to enclose the someServiceFunction() in a try-catch block?
Or, catching it in the Repository class enough?
When you catch an Exception, you got 2 options, manage the Exception right were you caught it, according to your app logic or needs or you can throw the exception again and propagate this Exception.
In your case you are throwing the exception again, so you need to catch it again in whatever place you called the method and if you keep throwing the exception up you have to keep catching it.

Handle specific Exceptions in php

I want to catch a specific Exception and handle it properly. However, I have not done this before and I want to do it in the best way.
Will it be correct to create a separate class something like
class HandleException extends Exception
{
//my code to handle exceptions;
}
and in it have different methods handling the different exception cases? As far as I understand, the Exception class is like an "integrated" class in php so it can be extended and if an Exception is caught it is not obligatory to terminate the flow of the program?
And, an instance of this class will be created when an exception is caught? Sth. like
catch ( \Exception $e ) {
$error = new HandleException;
}
You CAN extend the basic Exception object with your own, to provide your own exception types, e.g.
class FooExcept extends Exception { .... }
class BarExcept extends Exception { .... }
try {
if ($something) {
throw new FooExcept('Foo happened');
} else if ($somethingelse) {
throw new BarExcept('Bar happened');
}
} catch (FooExcept $e) {
.. foo happened, fix it...
} catch (BarExcept $e) {
... bar happened, fix it ...
}
If an Exception is caught, then the program DOESN'T necessarily have to abort. That'd be up to the exception handler itself. But if an exception bubbles always back up to the top of the call stack and ISN'T caught, then the entire script will abort with an unhandled exception error.
From the manual
Multiple catch blocks can be used to catch different classes of
exceptions. Normal execution (when no exception is thrown within the
try block) will continue after that last catch block defined in
sequence. Exceptions can be thrown (or re-thrown) within a catch
block.
So you can do something like this:
try {
// some code
} catch ( HandleException $e ) {
// handle sthis error
} catch ( \Exception $e ) {
// handle that error
}
This will handle different exceptions. You can also use the finally keyword with newer versions of PHP.

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.

Why isn't my Exception being caught by catch?

I have some code that looks like this
# Try to import file
try
{
DataManager::fileImport($_FILES['datafile']['tmp_name'],
$_POST['zones'], $_POST['statuses']);
}
catch(Exception $e)
{
print 'Herp.';
$response->body = Helpers::getVarDump($e);
}
DataManager::fileImport is literally a one-line function that throws a normal Exception:
static function fileImport($filepath, $zones, $statuses)
{
throw new Exception('SOME EXCEPTION');
}
And yet I get
Fatal error: Uncaught exception 'Exception' with message 'SOME EXCEPTION'...
From the try block. Also 'Herp.' is never printed. Why doesn't the Exception trigger the catch block?
EDIT: I should mention I'm using Tonic and PHP 5.3.9
EDIT AGAIN: Here's DataManager (with names replaced with ... for anonymity) http://pastebin.com/daHWBJDC
Solution
I neglected to specify use \Exception; in the file containing the try/catch.
Pondering
I know it's intentional that each namespace in PHP should define its own Exception for many reasons, but I still find it odd that catch(Exception e) didn't cause any errors when Exception in that context wasn't defined. If I were to write new Exception() I would get an error.
Oh well, at least I learned something.
Strange. If i run this code i get the "Herp."
<?php
class DataManagerTest {
static function fileImport($filepath, $zones, $statuses)
{
throw new Exception('SOME EXCEPTION');
}
}
# Try to import file
try
{
DataManagerTest::fileImport("param1","param2","param3");
}
catch(Exception $e)
{
print 'Herp.';
}
?>
You might have an issue with your DataManager class because i copied your code, adapted it to run and i get the exception handled... You problem is elsewhere...
class DataManager {
static function fileImport($filepath, $zones, $statuses){
throw new Exception('SOME EXCEPTION');
}
}
try{
DataManager::fileImport('', '', '');
}catch(Exception $e){
print 'Herp.';
}
Results in
Herp.
4 years later...
#Hubro, thank you for saving me with that namespace fix!
It does seem counterintuitive at first that it's necessary when throwing a root-level Exception, even though it ultimately makes sense in the general context of namespaces.
For anyone who doesn't want to utilize #Hubro's file-level fix:
use \Exception;
You could instead add the backslash in front of Exception in the higher level catch block:
} catch (\Exception $e) {
We could all benefit from someone smarter than me providing suggestions on best practices around defining a custom Exception for each namespace. Any takers?

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