Error Handling | C Programming Tutorial

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

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

  • @probexpd1916
    @probexpd1916 Год назад +2

    Great explanation - really turned the light on for me without overloading it with all the bells and whistles - thank you!

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

      You're very welcome, I'm really glad that you enjoyed the video style and explanation! :-)

  • @fifaham
    @fifaham 9 месяцев назад

    Excellent video, the worst thing that can happen is when the code halts at run time and we dont know what have happened. In this video we know what went wrong. Thank you Kevin.

  • @milanstojiljkovic4802
    @milanstojiljkovic4802 2 года назад +6

    Great explanation! Clear and concise, keep up the good work! ☺

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +2

      Thank you very much Milan! I'm glad to hear you enjoyed the explanation. 😀

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

    Thanks a lot ! I was trying to figure out how to do a log file for my programm and you answered almost all my questions !

  • @ailijic
    @ailijic 2 года назад +7

    It's best practice to save errno right after the function call. Many of the functions called in the example also set errno. The function fopen is easy because the return value tells you that you have an error. In many functions the return value says you might have an error and you need to check errno, e.g. strtol. In these cases you want to set errno to zero before the call.

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

      Thanks for the feedback! This is of course just a basic demo to introduce the concept, a best practices video would be a lot longer and would involve more ideas than what you’ve covered in your comment. For example fopen() might be “easy” because the return value tells us that the function fails, but we use ferror() to also get an error code that tells us why something failed so we can handle that specific reason differently. Again this is of course just a basic intro video so we don’t get into that.

  • @angelffg
    @angelffg 7 месяцев назад

    Great explanation! Your videos are some of the best I've seen! Thanks for sharing!

    • @PortfolioCourses
      @PortfolioCourses  7 месяцев назад +1

      You're welcome, I'm glad you enjoyed it! :-)

  • @ВиталийОвчаренко-и1н
    @ВиталийОвчаренко-и1н 6 месяцев назад

    Error handling in C programming can be implemented by checking return values from functions, as many functions in C return -1 or NULL in case of errors. By checking these return values, a programmer can detect errors and handle them accordingly. Additionally, creating separate code to handle specific exceptions that may arise can help in recovering from errors without increasing complexity or technical debt. It is important to prevent errors in the first place and handle them appropriately to ensure smooth execution of the program.

  • @viczii4464
    @viczii4464 3 месяца назад

    From errno manual:
    On some ancient systems, was not present or did not
    declare errno, so that it was necessary to declare errno manually
    (i.e., extern int errno). Do not do this. It long ago ceased to
    be necessary, and it will cause problems with modern versions of
    the C library.

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

    best sources of code. thank you portfolio courses and thank you Kevin browne.

  • @svenwaibel7007
    @svenwaibel7007 8 месяцев назад

    Hi, is "extern in errno" really needed? I do not have it in the code and it works just fine.

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

    Thanks, nice explanation

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

      You’re welcome John, I’m glad you enjoyed it! :-)

  • @sruthimajhi5610
    @sruthimajhi5610 11 месяцев назад

    Can we write this same program in a linux system

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

    What about like strerror_s?

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

      I think I may cover that in a future video. :-) They were added in C11 because strerror() isn't thread safe: en.cppreference.com/w/c/string/byte/strerror.

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

      @@PortfolioCourses yeah plus I wanted to know how to work around in Linux since it doesn't have strerror_s

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

      I think that if you're using the C11 version of C, it should be available, even on Linux? But I could be incorrect about that.

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

      @@PortfolioCourses nah it's Microsoft extensions only
      I guess it won't hurt just not using the _s ones

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

      @@tails_the_god So according to this it's in C11, so if the compiler on a Linux machine is using C11, it should be in there: www.blinkstar.cn/posts/2022/09/talking-about-strerror/. But yeah, if your code isn't multithreaded you don't need _s versions.

  • @Everythingzof
    @Everythingzof 3 года назад +4

    How does errno know that what failed was file opening? I know you put "if (fh == NULL)" there, but how does it know that file opening failed and not something else? Who sets the errno value

    • @PortfolioCourses
      @PortfolioCourses  3 года назад +4

      Great question! :-) It is actually the fopen() function that sets errno. It can also access errno and set it to what it wants to when there is an error, and that's what it does.

    • @otaxhu
      @otaxhu 10 месяцев назад

      ​@@PortfolioCourses so it only works with standard libc functions isn't it?