Laravel 5.5 Try / Catch is not working it's execute the exception handle - laravel

I am working with laravel 5.5 I have written a code with try and catch exception. But Try / catch is not manage exception handling. Exception execute on the Exception/handle.php
Here is code I am following
try {
App\Models\justDoIt::find(1);
} catch (\Exception $ex) {
dd($ex);
report($ex);
return false;
}
I would like to know why catch is not executed and error is display from the handle.php in report()
Here is the handle.php code
public function report(Exception $exception) {
echo "Handle";
dd($exception);
parent::report($exception);
}
Result
Handle
FatalThrowableError {#284 ▼
#message: "Class 'App\Http\Controllers\App\Models\justDoIt' not found"
#code: 0
#file: "D:\xampp7\htdocs\homeexpert_nik\app\Http\Controllers\HomeController.php"
#line: 21
#severity: E_ERROR
trace: {▶}
}
Result will show from the handle.php file.

Your code is throwing an error, not an exception. You're trying to use a class that doesn't exist and PHP is complaining about it by throwing a FatalThrowableError.
In PHP 5, this code would have resulted in a fatal error message being rendered in the browser, however in PHP 7 (Which is required for Laravel 5.5), PHP now throws errors just like they were exceptions. This allows applications to catch these errors, just like exceptions using try/catch blocks.
The reason the error is escaping your try/catch block is that an error is not an Exception, or a child of it. The object being thrown is an Error. Both the Exception and Error classes implement a common interface, Throwable.
Throwable
- Exception
- Error
Laravel's exception handler is written to catch both of these classes in order to display the error page you're seeing.
If you were to change your code to the following, you would find that the } catch (Throwable $e) { block should be executed:
try {
App\Models\justDoIt::find(1);
} catch (\Exception $ex) {
dd('Exception block', $ex);
} catch (\Throwable $ex) {
dd('Throwable block', $ex);
}
For more information on this, see here.
An added extra: If you're wondering what the issue is with your code, it's because you likely forgot to use your model class in your controller:
use App\Models\justDoIt;

Related

How to handle 'throw new DecryptException('The payload is invalid.')' on Laravel

I have small Laravel project working on Crypt class. It work fine for both Crypt::encrypt(..) and Crypt::decrypt(..). But I have problem if I directly change on crypted value then try to capture exception. For example, my crypted value is
zczc1234j5j3jh38234wsdfsdf214
Then I directly add some words as below.
zczc1234j5j3jh38234wsdfsdf214_addsometext
I try to decrypt and get error as below
throw new DecryptException('The payload is invalid.')
So, I try to capture exception with render method.
public function render($request, Exception $exception)
{
if ($exception instanceof \Illuminate\Contracts\Encryption\DecryptException) {
dd("error");
return route('login')->withError('Your DB may be hacked');
}
return parent::render($request, $exception);
}
I do not known why method not fire, Appreciated&thanks for all comment.
You should handle this with
use Illuminate\Contracts\Encryption\DecryptException;
try {
$decrypted = decrypt($encryptedValue);
} catch (DecryptException $e) {
//
}
check https://laravel.com/docs/5.8/encryption

Laravel 5.4 - unable to catch all exceptions

On Laravel 5.4, I'm using a console command of my own. This command is a scheduled task (every minute). This task instantiates a library. In this library, the very first thing I'm doing is:
try {
$this->doSomething1();
$this->doSomething2();
...
}
catch (Exception $e) {
...
}
catch (\Exception $e) {
...
}
Most of the times, the exceptions are caught, my code keeps running smoothly.
But sometimes, it isn't caught. For example, I had an exception yesterday because I was trying to insert a longtext into a text field in MySQL who complained about it. The exception was not raised and my command aborted.
What am I missing? How can I catch all the exception at once?
Thanks a lot,
Axel

How can i catch Container exceptions?

I want to be able to catch container binding exceptions but it doesn't seem to work.
If i have this code:
try {
$instance = app()->make('SomeNonExistingBinding');
} catch (Exception $e) {
// handle failure
}
But somehow the thrown Exception is being caught in the Illuminate\Routing\Pipeline::prepareDestination() method that AFAIK converts the exceptions in HTTP responses, instead of my try-catch block.
Can someone help me on this, please?
Thanks in advance.

Check smarty Syntax Error in Html

From php, Is there any way by which i can detect if there is any smarty syntax error in html ?
Just fetch the Template and catch exceptions.
try {
$template->fetch("test.tpl");
}
catch (Exception $e) {
}
You can place that in a function that returns whatever you need.

Unable to throw httpResponseException

When I try to throw the below forbidden ResponseException from my controller. An exception stating "Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details." is catched in the catch block of the controller method. Need help in resolving this
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
Just change your controller implementation to re-throw if it's an HttpResponseException:
try
{
// action implementation
}
catch (Exception e)
{
if (e is HttpResponseException)
{
throw e;
}
// error handling logic
}
But the better answer is that #1 - you should avoid be catching all exceptions, that's bad practice. And #2 - you should use an exception filter instead to do your error handling and not catch exceptions yourself.

Resources