Stop Using Exceptions in C#! Here’s Why and What to Do Instead

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

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

  • @simongottschlag2290
    @simongottschlag2290 Месяц назад

    Amazing Filip! 😊

  • @fakhrulhilal
    @fakhrulhilal Месяц назад

    I am using clean architecture pattern from Jason Taylor template. It's a great pattern. I am struggling when trying to avoid exception in MediatR pipeline. I tried to convert to Result class. And it's challenging to make the code more C# friendly rather than functional way.

  • @martinkrajda5521
    @martinkrajda5521 Месяц назад +1

    Nice, but you could have added the MemoryDiagnoser to see if there might be issues with GC.

    • @FilipEkberg
      @FilipEkberg  Месяц назад

      @@martinkrajda5521 Thats very true! Thanks for pointing that out.

    • @fakhrulhilal
      @fakhrulhilal Месяц назад

      @@FilipEkberg Yes, please include memory allocation by default when doing benchmark

    • @FilipEkberg
      @FilipEkberg  Месяц назад

      @@fakhrulhilalAlways! (Except this time when I forgot) 🫣

  • @EverRusting
    @EverRusting Месяц назад

    From C to C# then back to C, monads are fine if language is based on them. No reason to not use exceptions. Monad and exception code side by side is the same except you have unwrap opeator at the end.

  • @BigMaaaaaaan
    @BigMaaaaaaan Месяц назад +4

    I’m sorry but no. Of course one could argue that using exceptions for domain validation is an overkill as we are returning tons of data that is not needed in these situations. That’s true. And of course result pattern is more elegant and more suitable solution for this problem. But using performance as an argument in this discussion is absurd. I cannot imagine a real world example of an application that suffers from performance issues due to too many domain validation exceptions being thrown. If it is in fact the case then the problem lies in a poor solution architecture or execution, not in exceptions themselves.
    Don’t get me wrong, I am a fan of result pattern and often try to implement it. However I refuse to take performance as an argument in the exceptions vs result pattern debate.

    • @MrNatsuDragneel
      @MrNatsuDragneel Месяц назад

      Yes, and to resolve an error in the solution in production, you save thousands or hundreds of thousands of dollars because it was only 5 times slower when an exception is thrown, where this is ultimately a very rare occurrence and it should be Taken seriously, the stack trace allows the company's support team to quickly resolve the problem and save its application and money.

    • @FilipEkberg
      @FilipEkberg  Месяц назад

      The point is that using exceptions for flow control is bad design. It's very often used for domain validation, because a lot of people don't think it matters. But it does. It does come as a surprise to many that exceptions are costly.
      If you are working on a smaller project, sure, it may not matter at all. As with everything, use what works best for you. However, in demanding applications, it is just so extremely unnecessary to design your application using practices that are notoriously slow.

    • @FilipEkberg
      @FilipEkberg  Месяц назад +1

      @@MrNatsuDragneel For actual exceptions it’s a different story, obviously. When there’s something you haven’t prepared for that goes wrong, the stack trace is crucial.
      I think you are missing the point. I’m talking about avoiding exceptions for situations where it doesn’t make sense, like normal flow control. An example is validation.

  • @zivunknown
    @zivunknown Месяц назад +1

    Don't use excedptions for flowcontrol? Tell that to the Python devs... :/

    • @FilipEkberg
      @FilipEkberg  Месяц назад

      The python docs should make it more clear right away that it’s not a good idea:
      ”A try/except block is extremely efficient if no exceptions are raised. Actually catching an exception is expensive.”

  • @AL-kn4yx
    @AL-kn4yx Месяц назад +1

    Could you make a video with examples of where using exceptions would be appropriate?

    • @FilipEkberg
      @FilipEkberg  Месяц назад

      Thanks for the great suggestion!
      Exceptions are great for exceptional cases, when it's an unplanned outcome. There are definitely situations where it makes a lot of sense, but if it's a normal case that occasionally occurs then don't use exceptions.

  • @lextr3110
    @lextr3110 Месяц назад

    you should sync your audio better to yourself

    • @FilipEkberg
      @FilipEkberg  Месяц назад

      @@lextr3110 Where is it off? Shouldn’t be out of sync..

    • @emillarson89
      @emillarson89 Месяц назад

      @lextr3110
      Check your playback unit. Looks great on my end, and it’s probably timecode-synced during recording :)