Laravel and PHP Try-Catch: Exceptions VS Errors?

Поделиться
HTML-код
  • Опубликовано: 11 окт 2024

Комментарии • 47

  • @vitorfontoura5417
    @vitorfontoura5417 3 года назад +83

    I think it is worth mentioning that if you really want to catch any Exception or Error, you can catch Throwable, as both Errors and Exceptions do implement it.

    • @librasulus
      @librasulus 3 года назад +9

      Right! Great catch! (pun intended)

    • @LeTrolli
      @LeTrolli 3 года назад +1

      Yes !

    • @umarbabajidda9664
      @umarbabajidda9664 3 года назад +3

      Neat. We learn every day. I didn't know that, I always thought Throwable was just a generic for Exceptions

    • @passportmarc
      @passportmarc 3 года назад

      I didnt know we could use anything else lol !

    • @adarshchacko6137
      @adarshchacko6137 Год назад

      What is the difference between Throwable and Exception ?

  • @Kin0bi
    @Kin0bi 3 года назад +24

    Hi thanks for the channel.
    In some rare cases, I catch \Throwable which is an interface that includes Exception and Error I think.

  • @imranorthocare267
    @imranorthocare267 3 года назад +3

    Thank for the video. I never thought of catching the error but after this video I had to say small videos like this one can actually help us all to learn new thing.
    I really appreciate your efforts.

  • @emekatimothyiloba699
    @emekatimothyiloba699 2 года назад +1

    Thanks for all you do sir
    from Nigeria

  • @Realtek2super
    @Realtek2super 3 года назад +2

    Thanks a lot of, Polivas!! I didn't use /Error. I Think it is good to use it for logging. Or for API, to have a convenient message for front-end

  • @Towzlie
    @Towzlie 3 года назад +1

    Very useful 😀
    Error Handling is one of the most important things for UX

  • @nejuspesnejsi
    @nejuspesnejsi 3 года назад +6

    You have 2 inserts inside Try. It might be a good idea to do transaction & rollback to make error handling complete?

  • @nina_knyazeva
    @nina_knyazeva 2 года назад +1

    thanks a million! it's very useful for me! Your english is very clear)

  • @martinh4982
    @martinh4982 3 года назад +1

    I always recommend caution with the DivideByZero error - sometimes it won't work as you expect! PHP being a bit crackpot at times treats straight-up division by zero as a warning rather than an error, and this can't be caught. To remedy this, always check your inputs before attempting any division.

  • @kickass1179
    @kickass1179 Год назад +1

    What if we use Throwable which catches both General Exception and Errors?

  • @RANJEETKUMAR-wz4dg
    @RANJEETKUMAR-wz4dg 3 года назад +1

    thanks for very helpful topic

  • @saifudpopz8171
    @saifudpopz8171 3 года назад +1

    Can you please make a video of php arrow function using in laravel

  • @harijsg
    @harijsg 3 года назад +1

    Thank you for your videos and ideas, Povilas! Watching you almost every day. But I got a bit curious and started to wonder, why use 'App\Model\Thread' instead of Thread:: class, which would significantly reduce chance of typos. Noticed this in other examples and also in Laravel in general, so this is not any expression of negativity or so, just curious

    • @LaravelDaily
      @LaravelDaily  3 года назад +1

      That was just a random example when class name may be dynamic as a variable.

  • @zafarbekbadalboyev9689
    @zafarbekbadalboyev9689 3 года назад +1

    Hi Povilas, thanks for videos. Do you have a plan for making a video about Pest testing?

  • @FuriousDin
    @FuriousDin 3 года назад +3

    If you have Error you bad developer or you write bad testing code. Catch error is way to hide/miss problem. I see this many times when junior's catch erros and can't fix bug....

  • @neerajsinghtangariya2587
    @neerajsinghtangariya2587 3 года назад +2

    sir what are the use case of DB::transaction...?

    • @LaravelDaily
      @LaravelDaily  3 года назад

      ruclips.net/video/7daBdm2xgm8/видео.html

    • @neerajsinghtangariya2587
      @neerajsinghtangariya2587 3 года назад +1

      @@LaravelDaily Thank you now I understand the Use Case of DB::transaction....

  • @belce1982
    @belce1982 3 года назад +1

    Thanks as always!!

  • @javiershaka
    @javiershaka 3 года назад

    Personally, I don't use the try catch much in php because of the sequential flow, for me it is easier to throw new Exception for error handling, on the other hand when I program in java with swing or fx I use it a lot so that the environment does not trigger graphic.

  • @binodthakur696
    @binodthakur696 3 года назад +1

    what is the use of DB::transaction...?

    • @LaravelDaily
      @LaravelDaily  3 года назад +1

      ruclips.net/video/7daBdm2xgm8/видео.html

  • @konstruktion
    @konstruktion 3 года назад +1

    Hey! Just a quick question. I always write "return redirect()->to(route('post.index'));".
    With you I see "redirect()->route('xy');"
    Don't you need to set this "to" for routes? Does the "to" perhaps have another purpose?
    Thanks! ✨

    • @LaravelDaily
      @LaravelDaily  3 года назад +1

      No there's no need to write to(), not sure why you write it :)

  • @chameeradinesh1221
    @chameeradinesh1221 3 года назад +1

    dear sir, how we can check this $names[ ] array variables empty or not( when i'm uploading only one or two images) in here ?
    Post::create([
    'title' => $request->get('title'),
    'details' => $request->get('details'),
    'image1' => $names[0],
    'image2' => $names[1],
    'image3' => $names[2],
    ]);

    • @daleryanaldover6545
      @daleryanaldover6545 3 года назад +1

      if $names is always to be expected as an array then you could do something like:
      // names not empty
      if ($names != null)
      of if you are working with eloquent and collection:
      // names not empty
      if (!$names)
      or
      // counting names
      if ($names->count() == 0)
      I suggest you loop through the $names array to avoid repetition and hard coding.

    • @chameeradinesh1221
      @chameeradinesh1221 3 года назад

      @@daleryanaldover6545 problem is it cannot check in create method in store function. Inside create method cannot use if() .

    • @chameeradinesh1221
      @chameeradinesh1221 3 года назад

      @@daleryanaldover6545 done. Thanks for the helping

    • @TamilKingt
      @TamilKingt 2 года назад +1

      ​@@chameeradinesh1221 use mergeWhen() function.

  • @MohamedX119
    @MohamedX119 3 года назад

    respect ... live long King

  • @Stoney_Eagle
    @Stoney_Eagle 3 года назад +1

    I have encountered that divide by 0 error a long time ago and laravel catched it in develop mode.

    • @Towzlie
      @Towzlie 3 года назад +1

      Good to know ! Thank you

    • @max7579
      @max7579 3 года назад

      @@Towzlie😂

  • @santrideveloper4236
    @santrideveloper4236 3 года назад

    make tutorial laravel inertia reactjs. Please.

  • @ivanazim3945
    @ivanazim3945 3 года назад

    Hello there...
    I have 2 different blade view coming from 2 different controllers.
    How can I merge the two views into one pdf file with single click...

  • @miroslavisikiyski4876
    @miroslavisikiyski4876 3 года назад

    IDK, but sequentual catch blocks are not that readable (or at least not enjoyable for reading) and honestly they look ugly as hell.
    I do not see a benefit where the only difference will be in printing either string containing "Exception...." or "Error....." - if u need it user friendly just writhe sth like: "Problem occurred:..." and simply always catch for \Throwable. Both Exceptions and Errors in PHP implement this interface..
    Now if you have custom exception class and wish to do some custom logic if it's catched - then you must use another catch block, that's for sure

  • @zafarbekbadalboyev9689
    @zafarbekbadalboyev9689 3 года назад

    First comment