try/catch-block without any effect

Hi guys,

As I assume, try/catch should perform code which is defined in catch-block,if there is any error in try-block,correct?

Following code won’t do that.It will be thrown out error-in this case:


Getting unknown property: common\modules\lookup\models\LAnrede::anreden

Any ideas,how to fix this respectively what I did wrong?




<?php


use yii\helpers\Html;

use frontend\modules\bewerber\models\Bewerber;

use common\modules\basis\models\Person;

use common\modules\lookup\models\LAnrede;

use yii\base\ErrorException;

use kartik\widgets\Alert;


try {

    $query_one = LAnrede::find()->innerJoin('Person', 'l_anrede.id = Person.id_anrede')->one();

    $query_all = LAnrede::find()->innerJoin('Person', 'l_anrede.id = Person.id_anrede')->all();


    var_dump($query_one);

    var_dump($query_all);


    echo"<h3>Anrede_one:" . $query_one->anrede . "</h3>";

    $x = 1;

    foreach ($query_all as $attributes) {

        echo "<br><label>Anrede_all_$x:<br>" . $attributes->anreden . "</label>";

        $x++;

    }

} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab

    echo Alert::widget([//informiere den Anwender,daß kein Datensatz gefunden wurde

        'type' => Alert::TYPE_DANGER,

        'title' => 'Konfigurationsfehler',

        'icon' => 'glyphicon glyphicon-remove-sign',

        'body' => 'Dem angemeldeten User ist in der Mitarbeitertabelle kein Mitarbeiter zugeordnet<br><br>' . $error,

        'showSeparator' => true,

        'delay' => false

    ]);

}

?>



Does "anreden" member exist in LAnrede model?

No,it doesn’t!

That’s the reason why I programmed try/catch-block.

Furthermore, I programmed try/catch-block in each method of model/class LAnrede

I want to check yii’s behaviour with try/catch,but it doesn’t work!

P.S.: As an author of a book about yii2 it should be no problem solving this problem,shouldn’t it?

This is just an error on the level of programming that YOU have to take care of. The problem is yours. Don’t bother your end users by trying to handle it with try/catch mechanism.

I mean that the try/catch mechanism is not for debugging.

The problem lies between your ears.

What I mean by that is:

  1. Don’t attack people who tries to help you solve your problem.

  2. If you don’t get the answers you are looking for, ask a better question (with more details, or from different angle).

  3. Own your problem! You simply have to figure it out yourself. With or without the help from others.

Sorry,but he effectively wrote a book about yii. So,as I assumed, he is able to solve my problem.

This hint was not intented to attack him :unsure:

Furthermore,I got solution ;)

Catch-block was wrong!




...

} catch (\Exception $error) { //fange den schweren MySQL-Fehler ab

...



instead of


} catch (ErrorException $error) { //fange den schweren MySQL-Fehler ab



will intercept all errors in try-block

Error and Exception handling is a very important aspect of web application design. To be honest, I myself don’t have confidence in the way I’m doing it.

The following documents must be the very basics.

W3Schools.com - PHP Error Handling (https://www.w3schools.com/PhP/php_error.asp)

W3Schools.com - PHP Exception Handling (https://www.w3schools.com/PhP/php_exception.asp)

If you have other docs that you recommend, please post the links. > All

Here is Exception-class of yii2 with all its subclasses, which inherit!