Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 not able to catch Exception

I have tried to isolate this problem (to produce it outside my app), but I can't.

try {
    $has_cache = Cache::has($cache_key);
}
catch (DecryptException $e) {
    echo "No biggie";
    exit;
}

I also tried with a catch (Exception $e), the same thing happens.

Using this code, I get a DecryptException in the second line. How can this happen, it's in the try block?

Like I said, I tried to do the same on a clean project, but there it caught the exception, so I'm asking where could I have messed something up.

like image 729
duality_ Avatar asked Jan 13 '13 23:01

duality_


2 Answers

If your application is namespaced, you would need to use

catch(\Exception $e);
// or preferably
catch(\RuntimeException $e);

likewise, I think the DecryptException you are trying to catch is namespaced in Illuminate\Encryption so you'd need

catch(\Illuminate\Encryption\DecryptException)
// or use "use" somewhere before the try/catch
use \Illuminate\Encryption\DecryptException

Keep in mind that Laravel 4 is still alpha or pre-beta (apparently they are not sure themselves), so it is in no way stable and probably not the best choice for production.

like image 195
dualed Avatar answered Oct 01 '22 11:10

dualed


For laravel 5.1 you should write(generally at start of the file with other use statements):

use Illuminate\Contracts\Encryption\DecryptException;

Before catch statement:

try {
    $data = \Crypt::decrypt($key);
} catch (DecryptException $e) {
    echo 'caught exception';
    exit();
}

Ref: https://laravel.com/docs/5.1/encryption - under "Decrypting A Value"

May be helpful for others.

like image 41
Abhishek Sachan Avatar answered Oct 01 '22 13:10

Abhishek Sachan