Ausnahme fängt im Try/Catch-Block nicht ab

Guerilla

Gibt es einen Grund, warum ein Try/Catch-Block nicht funktioniert?

Ich rufe eine Methode in der Magento2-API in einem try-Catch-Block auf, aber sie fängt die Ausnahme nicht ab und die Ausführung stoppt und ich sehe Stack-Trace.

Hier ist mein Code:

        $productModel = $this->productRepository->getById( $pId );

        $debpt = "";
        try{
            $debpt = $productModel->getCustomAttribute('depart_num')->getValue();
        }
        catch(\Exception $ex){
            $debpt = $ex->getMessage();
        }

Die getValue()Methode verursacht die Ausnahme, aber mein try/catch fängt sie nicht ab.

Hier ist der Stacktrace:

( ! ) Fatal error: Uncaught Error: Call to a member function getValue() on null in /var/www/nwl/app/code/Raleigh/CustomReports/Controller/Report/Generate.php on line 118
( ! ) Error: Call to a member function getValue() on null in /var/www/nwl/app/code/Raleigh/CustomReports/Controller/Report/Generate.php on line 118
Call Stack
#   Time    Memory  Function    Location
1   0.0093  383800  {main}( )   .../index.php:0
2   1.2357  12558448    Magento\Framework\App\Bootstrap->run( ??? ) .../index.php:39
3   1.2362  12560232    Magento\Framework\App\Http->launch( )   .../Bootstrap.php:258
4   2.3292  22376480    Magento\Framework\App\FrontController\Interceptor->dispatch( ??? )  .../Http.php:135
5   2.3302  22464168    Magento\Framework\App\FrontController\Interceptor->___callPlugins( ???, ???, ??? )  .../Interceptor.php:26
6   2.3302  22467712    Infortis\Cgen\Plugin\Magento\Framework\App\FrontController->aroundDispatch( ???, ???, ??? ) .../Interceptor.php:142
7   2.3305  22469480    Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}( ??? )  .../FrontController.php:32
8   2.3305  22469856    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Interceptor.php:138
9   2.3313  22499968    Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch( ???, ???, ??? )  .../Chain.php:67
10  2.3314  22501304    Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}( ??? )   .../BuiltinPlugin.php:68
11  2.3314  22501680    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Chain.php:63
12  2.3315  22504744    Magento\PageCache\Model\App\FrontController\VarnishPlugin->aroundDispatch( ???, ???, ??? )  .../Chain.php:67
13  2.3315  22504744    Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}( ??? )   .../VarnishPlugin.php:55
14  2.3315  22505120    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Chain.php:63
15  2.3318  22517640    Magento\Framework\Module\Plugin\DbStatusValidator->aroundDispatch( ???, ???, ??? )  .../Chain.php:67
16  2.4050  22671616    Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}( ??? )   .../DbStatusValidator.php:69
17  2.4050  22671992    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Chain.php:63
18  2.4053  22677224    Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch( ???, ???, ??? )   .../Chain.php:67
19  2.4067  22697128    Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}( ??? )   .../RequestPreprocessor.php:94
20  2.4067  22697504    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Chain.php:63
21  2.4067  22697544    Magento\Framework\App\FrontController\Interceptor->___callParent( ???, ??? )    .../Chain.php:70
22  2.4067  22697544    Magento\Framework\App\FrontController\Interceptor->dispatch( ??? )  .../Interceptor.php:74
23  2.6148  27411640    Raleigh\CustomReports\Controller\Report\Generate\Interceptor->dispatch( ??? )   .../FrontController.php:55
24  2.6158  27438408    Raleigh\CustomReports\Controller\Report\Generate\Interceptor->___callPlugins( ???, ???, ??? )   .../Interceptor.php:26
25  2.6303  29018384    Magento\Tax\Model\App\Action\ContextPlugin->aroundDispatch( ???, ???, ??? ) .../Interceptor.php:142
26  2.6303  29018384    Raleigh\CustomReports\Controller\Report\Generate\Interceptor->Magento\Framework\Interception\{closure}( ??? )   .../ContextPlugin.php:91
27  2.6303  29018760    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Interceptor.php:138
28  2.6367  30050392    Magento\Weee\Model\App\Action\ContextPlugin->aroundDispatch( ???, ???, ??? )    .../Chain.php:67
29  2.6367  30050392    Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}( ??? )   .../ContextPlugin.php:112
30  2.6367  30050768    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Chain.php:63
31  2.6368  30052808    Magento\Store\App\Action\Plugin\StoreCheck->aroundDispatch( ???, ???, ??? ) .../Chain.php:67
32  2.6368  30052808    Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}( ??? )   .../StoreCheck.php:44
33  2.6368  30053184    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Chain.php:63
34  2.6369  30055776    Magento\Customer\Model\App\Action\ContextPlugin->aroundDispatch( ???, ???, ??? )    .../Chain.php:67
35  2.6372  30081328    Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}( ??? )   .../ContextPlugin.php:61
36  2.6372  30081704    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Chain.php:63
37  2.6373  30085880    Magento\Store\App\Action\Plugin\Context->aroundDispatch( ???, ???, ??? )    .../Chain.php:67
38  2.6448  30193896    Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}( ??? )   .../Context.php:106
39  2.6448  30194272    Magento\Framework\Interception\Chain\Chain->invokeNext( ???, ???, ???, ???, ??? )   .../Chain.php:63
40  2.7299  30787752    Raleigh\CustomReports\Controller\Report\Generate\Interceptor->___callParent( ???, ??? ) .../Chain.php:70
41  2.7299  30787752    Raleigh\CustomReports\Controller\Report\Generate\Interceptor->dispatch( ??? )   .../Interceptor.php:74
42  2.7385  31511040    Raleigh\CustomReports\Controller\Report\Generate\Interceptor->execute( )    .../Action.php:102

Ich bin hier ratlos, wie ich das angehen soll. Die API-Klasse hat keine Methode, um nach einem Wert zu suchen, um den Nullfehler zu vermeiden, und mein try/catch-Block funktioniert einfach nicht.

Weiß jemand, warum Try/Catch nicht funktioniert?

ishegg

Der Code wirft kein Exception, er wirft ein Error, deshalb konnten Sie ihn nicht fangen.

Sie können zum Beispiel zuerst prüfen, ob $productModel->getCustomAttribute('depart_num') null ist

if ($productModel->getCustomAttribute('depart_num') !== null) { // do your logic

Alternativ, wenn Sie PHP 7+ verwenden (was ich denke, dass Sie es sind), können Sie ändern, was Sie fangen, da Errors jetzt abfangbar sind :

catch (\Error $e) {

Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.

Bei Verstößen wenden Sie sich bitte [email protected] Löschen.

bearbeiten am
0

Lass mich ein paar Worte sagen

0Kommentare
LoginNach der Teilnahme an der Überprüfung

Verwandte Artikel

Verschachtelter Try-Catch-Block fängt Ausnahme nicht ab

Try Block fängt falsche Ausnahme ab

doOnError fängt die Ausnahme nicht ab

Try block löst eine Ausnahme aus, catch catch fängt sie jedoch nicht ab

Warum fängt der Try-Catch-Block die Versprechen-Ausnahme nicht ab?

@ try @ catch-Block fängt keine innere Ausnahme ab

Warum fängt "catch" diese Ausnahme nicht ab?

assertRaises in unittest fängt Ausnahme nicht richtig ab

TryCatch Decorator fängt keine Fehler ab

Warum fängt dieser außer Block FileExistsError nicht ab?

Groovy catch-Block fängt MultipleCompilationErrorsException nicht ab

Warum fängt eine mehrfach vererbte Ausnahme die übergeordnete Ausnahme nicht ab?

Der C ++ Try-Catch-Block fängt keine Hardware-Ausnahme ab

Promise.catch () fängt im AngularJS-Komponententest keine Ausnahme ab

@ try / @ catch fängt keine Ausnahme ab

UncaughtExceptionHandler fängt keine Ausnahme ab

Python fängt eine benutzerdefinierte Ausnahme ab

Try / Catch fängt keine Ausnahme ab

Oracle fängt keine Ausnahme ab

Sendet eine Nachricht an eine nicht vorhandene Börse und fängt eine Ausnahme ab

Java-Ausnahme fängt nicht ab, Code wird weiter ausgeführt

Die mit @ControllerAdvice kommentierte Klasse fängt die in der Serviceschicht ausgelöste Ausnahme nicht ab

Warum fängt C ++ try / catch die Ausnahme "reiner virtueller Aufruf" nicht ab?

Die Assert.Throws-Methode fängt die erwartete Ausnahme nicht ab

tryCatch fängt keinen Fehler ab und überspringt das Fehlerargument

Mein Regex fängt das erforderliche Muster im Text nicht ab.

Mein Versprechen fängt eine Ausnahme ab und ich weiß nicht, wie ich die Fehlermeldung drucken soll

Ausnahme-Handler-Middleware fängt nicht

Laravel-Ausnahme fängt nicht an