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
Related
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
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.
try {
if ($user_session_object['device_type'] == '1') {
$this->sendNotificationAndroidOne($notificationData);
}
throw new Exception("newInner");
//throw new Exception("newInner");
if ($user_session_object['device_type'] == '2') {
$this->sendNotificationIphone($notificationData);
}
}catch(Exception $ex){
echo 'Exception occur while send push notification';
exit;
}
Getting error: Exception' not found in file. Thanks in advance
This is likely a namespace issue (nothing to do with Zend Framework), so you want throw new \Exception("newInner"); and catch(\Exception $ex).
Otherwise, please add the full error message to your question.
As per your code "throw new Exception("newInner")" it have not makes any sence inside try{} block, because whenever an exception will be found catch(){} block will be called.
You should use try---catch like below.
try{
if ($user_session_object['device_type'] == '1') {
$this->sendNotificationAndroidOne($notificationData);
}
if ($user_session_object['device_type'] == '2') {
$this->sendNotificationIphone($notificationData);
}
}catch (\Exception $e) {
var_dump($e->getMessage());
var_dump($e->getCode());
exit;
}
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).
Suppose to have a PHP code inside a try...catch block. Suppose that inside catch you would like to do something (i.e. sending email) that could potentially fail and throw a new exception.
try {
// something bad happens
throw new Exception('Exception 1');
}
catch(Exception $e) {
// something bad happens also here
throw new Exception('Exception 2');
}
What is the correct (best) way to handle exceptions inside catch block?
Based on this answer, it seems to be perfectly valid to nest try/catch blocks, like this:
try {
// Dangerous operation
} catch (Exception $e) {
try {
// Send notification email of failure
} catch (Exception $e) {
// Ouch, email failed too
}
}
You should not throw anything in catch. If you do so, than you can omit this inner layer of try-catch and catch exception in outer layer of try-catch and process that exception there.
for example:
try {
function(){
try {
function(){
try {
function (){}
} catch {
throw new Exception("newInner");
}
}
} catch {
throw new Exception("new");
}
}
} catch (Exception $e) {
echo $e;
}
can be replaced to
try {
function(){
function(){
function (){
throw new Exception("newInner");
}
}
}
} catch (Exception $e) {
echo $e;
}
You have 2 possible ways:
You exit the program (if it is severe) and you write it to a log file and inform the user.
If the error is specifically from your current class/function,
you throw another error, inside the catch block.
You can use finally. Code in this branch will be executed even if exception is thrown within catch branch