Yii Framework Forum: try/catch-block without any effect - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

try/catch-block without any effect [solved]

#1 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 21 September 2017 - 09:31 AM

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
    ]);
}
?>

0

#2 User is offline   Fabrizio Caldarelli 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,073
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 21 September 2017 - 09:34 AM

Does "anreden" member exist in LAnrede model?
Yii2 by example: my book about Yii2
https://www.packtpub...nt/yii2-example
0

#3 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 21 September 2017 - 09:54 AM

View PostFabrizio Caldarelli, on 21 September 2017 - 09:34 AM, said:

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?
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,642
  • Joined: 16-February 11
  • Location:Japan

Posted 22 September 2017 - 09:49 AM

Quote

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

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.
1

#5 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,710
  • Joined: 10-October 10
  • Location:Denmark

Posted 22 September 2017 - 12:41 PM

View Posttklustig, on 21 September 2017 - 09:54 AM, said:

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

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.
"Less noise - more signal"
1

#6 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 23 September 2017 - 09:10 AM

View Postjacmoe, on 22 September 2017 - 12:41 PM, said:

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.



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
0

#7 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,642
  • Joined: 16-February 11
  • Location:Japan

Posted 23 September 2017 - 09:16 AM

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.w3school...P/php_error.asp)
W3Schools.com - PHP Exception Handling (https://www.w3school...p_exception.asp)

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

#8 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 24 September 2017 - 01:33 AM

View Postsoftark, on 23 September 2017 - 09:16 AM, said:

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!
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users