Error handling with try and catch in Laravel - php

I want to implement a good error handling in my app, I have forced this file for catching the error.
App\Services\PayUService
try {
$this->buildXMLHeader; // Should be $this->buildXMLHeader();
} catch (Exception $e) {
return $e;
}
App\Controller\ProductController
function secTransaction(){
if ($e) {
return view('products.error', compact('e'));
}
}
And this is what I get.
I don't know why Laravel is not redirecting me to the view.
Is the error forced right?

You are inside a namespace so you should use \Exception to specify the global namespace:
try {
$this->buildXMLHeader();
} catch (\Exception $e) {
return $e->getMessage();
}
In your code you've used catch (Exception $e) so Exception is being searched in/as:
App\Services\PayUService\Exception
Since there is no Exception class inside App\Services\PayUService so it's not being triggered. Alternatively, you can use a use statement at the top of your class like use Exception; and then you can use catch (Exception $e).

Related

How to rethrow an exception and call a method based on the original exception in php?

I am new to PHP and trying to active below:
I am catching multiple custom exceptions including standard exception.
I want throw these exceptions as another custom exception and based on the original exception I want to print a message.
example.php
try {
// Do something
} catch (CustomeException1 $ex) {
} catch (CustomException2 $ex) {
} catch (Exception $ex) {
}
I want to write something like FinalCustomException.php
class FinalCustomException extends Exception {
public static fromException(CustomException1 $ex) {
//print something
}
public static fromException(CustomException2 $ex) {
//print something
}
public static gotException() {
//call above fromException method based on the exception occurred
}
}
I am not sure How to do this. Can anyone please help me with this?
Thanks in advance.

Exception not able to get caught in catch block - Yii2

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.

How to catch SQL Exception in Laravel 5

Hay I'm creating a code like this :
\Log::info("saving log....");
try{
$data = AstronautCandidateLog::insert($request->logs);
}catch (SQLException $e)
{
\Log::info("SQL Exception happened");
}catch (Exception $e)
{
\Log::info("Exception happened");
}
\Log::info("status save data : ". $data);
But it seems that my Exception never get hit. So how to capture exception in laravel when something wrong in sql query...??
Thanks in advance.
Try this
try {
//Your code
} catch(\Illuminate\Database\QueryException $ex){
dd($ex->getMessage());
}
OR
use Illuminate\Database\QueryException;
try {
//Your code
} catch(QueryException $ex){
dd($ex->getMessage());
}
My case: add \ before Exception class otherwise it not handle
try
{
//write your codes here
}
catch(\Exception $e) {
Log::error($e->getMessage());
}
Make sure to use the Exception library in your controller. From there you can do:
try{
Some queries . . .
}catch(Exception $e){
return response()->json([
'error' => 'Cannot excecute query',
],422);
}
To implement an exception in Laravel, simply include the Exception class, at the top of your controller as
Use Exception;
Then wrap the lines of code you wish to catch an exception on using try-catch statements
try
{
//write your codes here
}
catch(Exception $e)
{
dd($e->getMessage());
}
You can also return your errors in json format, incase you are making an Ajax request, this time, remember to include the Response class at the top of your controller
Use Response;
Use Exception;
then wrap your codes in a try-catch statement as follows
try
{
//write your codes here
}
catch(Exception $e)
{
return response()->json(array('message' =>$e->getMessage()));
}
I hope this will solve your problem, you can learn more on Laravel and Error handeling here

PHP try-catch some problems

Hello. Is it possible to use such code in PHP?
try {
throw new InternalException('Internal');
} catch (InternalException $e) {
throw new Exception('Internal To global');
} catch (Exception $e){
print $e->getMessage();
}
class InternalException extends Exception {
// some code here
}
Nest multiple try...catch instead.
try {
throw new InternalException('Internal');
} catch (InternalException $e) {
try {
throw new Exception('Internal To global');
} catch (Exception $e){
print $e->getMessage();
}
}
class InternalException extends Exception {
// some code here
}
See PHP: Exceptions - Manual
It does not make sense to "transform" Exceptions. Don't throw em if you won't handle them.
You can catch different Exceptions this way:
try {
throw new InternalException();
} catch (HardwareException $e) {
} catch (InternalException $e) {
// this catch block will be executed
} catch (Exception $e) {
// all other exceptions
}
Yes, you can catch specific exceptions separately.
Exceptions are only caught if they're thrown in try blocks. Exceptions thrown in catch blocks are not caught within other sibling catch blocks of the same try..catch statement. You have to nest the whole thing in another outer try..catch block to catch those.

How to catch exceptions in your ZF2 controllers?

I've setup the ZendSkeletonApplication with ZF 2.0.3 and I am unable to catch exceptions in my controllers. For instance if I put the below piece of code in module/Application/src/Application/Controller/IndexController.php:
public function indexAction() {
echo "BEFORE\n";
try {
throw new \Exception("My exception");
} catch (Exception $e) {
echo "Caught exception $e\n";
exit;
}
and access the page I get:
BEFORE
An error occurred
An error occurred during execution; please try again later.
Additional information:
Exception
File:
module/Application/src/Application/Controller/IndexController.php:25
Message:
My exception
the ViewModel kicks in and displays the exception, effectively preventing me from catching it.
How can I catch exceptions in ZF2 controllers?
You are throwing PHP's generic Exception
throw new \Exception("My exception");
but you catch the Exception from the current namespace
} catch (Exception $e) {
Assuming your controller is in Application\Controller, you either have to declare
use \Exception;
above your class to import the global Exception into the current namespace or
} catch (\Exception $e) {
to catch PHP's global Exception.

Categories