i have this code:
<?php
[...]
// Authentication error
catch(AuthException $e)
{
// TODO: Save fault to auth into DB
// Propagate exception
throw $e;
}
// Common exception
catch(Exception $e)
{
// TODO: Log error here
$response = JsonResponse(array("error" => $e->getMessage()));
$response->send();
}
I want to able to throw AuthException and Exception, and want to be sure that AuthException's catch will store error into db, and as common Exception will be returned as error.
But now I have error, that I have uncaugth exception.
Or maybe I need to log error in the AuthException's constructor?
You don't need to catch each exception, I guess you wanna do something like this ?
try {
//...
} catch(Exception $e){
if($e instanceof AuthException ) {
// Log error here
}
$response = JsonResponse(array("error" => $e->getMessage()));
$response->send();
}
Related
Exception
Exception 'Error' with message 'Class 'app\commands\CallLogs' not
found'
is not able to get caught in catch block.
Code:
I tried with calling undefined class just to see how and what exception catch block catches.
public function actionTest(){
try {
$logs = new CallLogs();
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
But, When I intentionally throw any exception, it works.
public function actionTest(){
try {
throw new \yii\base\Exception('hello');
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
I have tried with base\Exception class and \ErrorException class. But, no help.
Any help/hint is appreciable
catch (\Throwable $e) will do the job
\Throwable was introduced back in PHP 7.0 and is (quoting from docs) used for
[...] any object that can be thrown via a throw statement, including
Error and Exception.
I am trying to catch the exception, but couldnt, just am getting the error message
try{
echo 'test';
require_once "jj.php";
return true;
} catch (Exception $ex) {
throw new Exception("error occured");
}
Warning: require_once(jj.php): failed to open stream: No such file or directory in C:\xampp\htdocs\corporate-wellness\module\Survey\src\Survey\Repository\QuestionRepository.php on line
That's not an exception (but a warning), so you can't simply catch it.
You can suppress warning instead (not recommended in this scenario) or use something to verify if file exists
So, something like
try {
if(!file_exists("jj.php")) {
throw new Exception("File doesn't exists");
}
require_once "jj.php";
return true;
} catch (Exception $ex) {
// if you would you can handle exception here or you can simply
// throw exception without try - catch block
}
I have the following script.
According to http://php.net/manual/en/class.pdoexception.php, You should not throw a PDOException from your own code.
But I want the same catch to be performed whether a PDOException or the exception that I threw for an invalid foo.
I've also been told that I should never catch the generic Exception, but only catch specific Exceptions.
How should this be implemented?
try {
db::db()->beginTransaction();
//Do a bunch of queries, and a PDO exception will be thrown upon error
if($foo($bar)) {throw new Exception('Invalid foo.');}
db::db()->commit();
} catch (PDOException $e) {
db::db()->rollBack();
//Maybe do some other stuff
}
Something like
try {
db::db()->beginTransaction();
//Do a bunch of queries, and a PDO exception will be thrown upon error
if($foo($bar)) {throw new RuntimeException('Invalid foo.');}
db::db()->commit();
} catch (PDOException $e) {
db::db()->rollBack();
//Maybe do some other stuff
} catch (RuntimeException $e) {
//foo invalid
}
I wonder if it's posible to get all the exceptions throwed.
public function test()
{
$arrayExceptions = array();
try {
throw new Exception('Division by zero.');
throw new Exception('This will never get throwed');
}
catch (Exception $e)
{
$arrayExceptions[] = $e;
}
}
I have a huge try catch block but i want to know all the errors, not only the first throwed. Is this possible with maybe more than one try or something like that or i am doing it wrong?
Thank you
You wrote it yourself: "This will never get throwed" [sic].
Because the exception will never get thrown, you cannot catch it. There only is one exception because after one exception is thrown, the whole block is abandoned and no further code in it is executed. Hence no second exception.
Maybe this was what the OP was actually asking for. If the function is not atomic and allows for some level of fault tolerance, then you can know all the errors that occurred afterwards instead of die()ing if you do something like this:
public function test()
{
$arrayExceptions = array();
try {
//action 1 throws an exception, as simulated below
throw new Exception('Division by zero.');
}
catch (Exception $e)
{
//handle action 1 's error using a default or fallback value
$arrayExceptions[] = $e;
}
try {
//action 2 throws another exception, as simulated below
throw new Exception('Value is not 42!');
}
catch (Exception $e)
{
//handle action 2 's error using a default or fallback value
$arrayExceptions[] = $e;
}
echo 'Task ended. Errors: '; // all the occurred exceptions are in the array
(count($arrayExceptions)!=0) ? print_r($arrayExceptions) : echo 'no error.';
}
In case a request cannot be made, how can I catch the exception?
I have so far written this, but not had any luck.
$url = 'http://localhost:49000/';
//create the httprequest object
try{
$httpRequest_OBJ = new httpRequest($url, HTTP_METH_POST, $options);
}catch(HttpException $e){
echo $e;
}
I had the same problem, you should make sure that the exception is in the namespace.
What I've done is:
try {
//Request here
} catch (\HttpException $e) {
//Handle exception
} catch (\Exception $e) {
//Just in case
}
Also good to notice what is pointed in this article http://www.mkfoster.com/2009/01/06/how-to-pecl-http-request-exception-and-error-handling/ about the inner exception, in case you want the message returned by the exception.
Also, there is a php.ini directive that forces HttpRequest to always throw exception http://br.php.net/manual/en/http.configuration.php http.only_exceptions=1
how can I catch the exception?
To catch an exception you need to know the classname of the exception. What is the classname of the exception you'd like to catch?