The Importance of Error Handling in C

Sdílet
Vložit
  • čas přidán 20. 12. 2023
  • Error handling is one of the most important things you need to do in order to write good production level C code
    You can get the sysinternals suite from the Windows Store or with winget package manager.
  • Věda a technologie

Komentáře • 112

  • @rocknowradio
    @rocknowradio Před 5 měsíci +15

    "That is because C does not come with built-in exception support". Thanks God for that.

  • @pavfrang
    @pavfrang Před 5 měsíci +85

    Your video is GREAT! I would only comment that it would be preferable to print to stderr instead of stdout (printf), for critical errors that terminate the application. So, you should use fprintf(stderr, ...) instead.
    The main reason for this, is that different redirection is typically done for stderr and stdout - the user might want to handle error messages differently from the informational messages.

    • @nirlichtman
      @nirlichtman  Před 5 měsíci +22

      This is a great point, thanks!

    • @ilmannen
      @ilmannen Před 5 měsíci +22

      Another suggestion is to use perror (also prints to stderr), which provides the message from the errno to the user without them having to look it up.

    • @rian0xFFF
      @rian0xFFF Před 5 měsíci +7

      I just use perror()

    • @blacklistnr1
      @blacklistnr1 Před 5 měsíci +4

      I've felt the lack of a separate error channel because I thought "just put everything into stdout, so much easier!" in a project:
      * the stdout quickly became unreadable and broke formatting
      * it also broke the REPL which was using stdin/out
      => errors needed special integration to be readable so "just put errors into stderr, so much easier!" :))

    • @pavfrang
      @pavfrang Před 5 měsíci

      yep perror the same, I just wanted to emphasize writing to stderr@@ilmannen

  • @kinershah464
    @kinershah464 Před 26 dny

    So much cool information. Very important error handling, right error handling with right tools and you can avoid hours of debugging an issue.

  • @michaelhohmann2868
    @michaelhohmann2868 Před 5 měsíci +1

    I really appreciate your focus on important and interesting information.
    There is no unnecessary spam and begging for likes and subscriptions.
    I immediately liked and subscribed 8-)

  • @sputnick1
    @sputnick1 Před 5 měsíci +7

    Great video! this is actually a big issue i certainly had in my first few c programs before i had enough experience to know better. One thing to note is the perror function. If you call perror, not only will it print to stderr (always print to stderr instead of stdout for errors), but it will also give you a description of the error number encountered. (the same text from the errno command)

  • @alielmessaoudi4863
    @alielmessaoudi4863 Před 5 měsíci +3

    Very valuable and interesting content, can't stop watching your videos, absolute value here

  • @johnshaw6702
    @johnshaw6702 Před 5 měsíci +1

    Thanks for presenting this. One of the things that always botherd me is progaming books that say they are leaving out the error checking to simplify the code for understanding, which it does, was that they often leave out a section on proper error checking and handling. When I first taught myself programming, I did a lot of research and reading (before the internet). I didn't fully trust anything and made sure that if my program failed it didn't crash the system, barring a system/hardware failure.
    Then modern systems like Windows came out and all bets were off. I could still write good code, but now I was more dependent on the Windowing system to not introduce errors, like memory leaks that forced me to write work arounds.

  • @r3v0lv3rz
    @r3v0lv3rz Před 5 měsíci

    Very clear and a great explanation of all concepts on your part. Thanks.

  • @uberbaud
    @uberbaud Před 5 měsíci +9

    Instead of calling gcc or clang directly, you can call 'make prog' and it will compile prog.c using environment variables CFLAGS LDFLAGS and LDLIBS. No Makefile required. You can see exactly the calls make will use with 'make -p'. One benefit is no a.out, and it's usually less typing.

    • @erc0re526
      @erc0re526 Před 5 měsíci

      Its amazing that it works. Now I must ask, how did you find this out? I didn't see any mention of it in the manpage... Thank you for this tip!

    • @uberbaud
      @uberbaud Před 5 měsíci +1

      @@erc0re526 In the opening paragraph of OpenBSD's make(1) man page, it says, " If neither of these exist, make can still rely on a set of built-in system rules."
      One of the many reasons I like OpenBSD is the quality of its documentation.
      Notably, GNU make has the same behavior but their manpage incorrectly states that "you must write a file called the makefile".

    • @erc0re526
      @erc0re526 Před 5 měsíci

      @@uberbaud One more reason for me to try OpenBSD! That's fascinating. Thank you

  • @UliTroyo
    @UliTroyo Před 5 měsíci

    Your C videos are so good! Thanks!

  • @StefanWelebny
    @StefanWelebny Před 5 měsíci +1

    Again valuable content. And thank you very much for answering my question about port connection concurrency regarding your last video!

  • @imod5
    @imod5 Před 5 měsíci

    I am currently working on an actual c program for the first time and this video showed in my home page at the right time. Thanks!

    • @johnshaw6702
      @johnshaw6702 Před 5 měsíci +1

      Check everything unless you know for a fact it can't fail and even then you may want to check. If you're not sure about something, look it up and verify. I self taught myself in the late 80's and early 90's and always double checked everything. Return values are there for a reason.
      Programmer's mantra: Trust but Verify.

    • @imod5
      @imod5 Před 5 měsíci +1

      @@johnshaw6702 That is a very valuable tip. Thank you for sharing!

  • @TheMasterAbdul
    @TheMasterAbdul Před 5 měsíci

    Very amazing videos, what a treasure

  • @umairmuhammadabbas3929
    @umairmuhammadabbas3929 Před 5 měsíci

    Thanks for sharing this information

  • @Not_Even_Wrong
    @Not_Even_Wrong Před 5 měsíci

    Thanks. I need to learn more about this, even good books don't convert it at all.

  • @anon_y_mousse
    @anon_y_mousse Před 5 měsíci

    I didn't even know there was an errno program. I thought maybe you wrote it yourself but looked it up and found it in moreutils. I'll have to recommend this to anyone learning C.

  • @steveoc64
    @steveoc64 Před 4 měsíci

    Be interesting to do a retake of this using zig, as it’s a good demo of how zig applies error handling to existing C idioms, without adding a tonne of boilerplate or weird abstractions.
    The error backtrace is particularly handy as well.

  • @mohammedsalman3397
    @mohammedsalman3397 Před 5 měsíci +4

    If you're on vim you can do shift-K to bring up the manual for the function your cursor is on

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      That is a cool tip, thanks! Just notice that it opens on full screen and so if you want to open the manual in split screen you can either use the :term like I do in the video or the :Man vim command which opens the manual as a Vim buffer

    • @darknais
      @darknais Před 5 měsíci

      man thats a god hack!! in my coding campus we dont have access to internet during exam only man pages this going to save me some time!

    • @mohammedsalman3397
      @mohammedsalman3397 Před 5 měsíci +1

      @@nirlichtman Yeah, forgot that it takes over the entire screen by default 😅I use neovim which opens the man page in a split window which I assume it's not impossible to do in vim.

  • @nathanthompson506
    @nathanthompson506 Před 5 měsíci

    this was brilliant. thanks

  • @darknais
    @darknais Před 5 měsíci

    Thanks for the video!

  • @acdimalev8405
    @acdimalev8405 Před 5 měsíci

    These days, rather than prototyping without error handling I lean into explicitly terminating the program.
    void _panic(int line)
    { printf("PANIC: %d
    ", line); exit(-1); }
    #define panic _panic(__LINE__)
    This helps with finding problems early without littering the program with boilerplate, and later it's useful for identifying places that I may want to add additional error handling logic.
    As a side bonus, it's also an active deterrant against internalizing an active ignorance of error handling. With how much time I spend prototyping, it's foolish to think it will be easy to mode-switch out of the habits I develop while prototyping.

  • @raccoon1160
    @raccoon1160 Před 5 měsíci +7

    Great video, I see way too much code that just ignores errors. Zero initializing (char buf[256] = {0}) is also standard in C89

    • @gblargg
      @gblargg Před 5 měsíci

      Then there's code that lumps all errors together. Disk full? Read-only filesystem? Out of memory? File not found? User value out of range? All the same error reported to the user!

  • @meni181818
    @meni181818 Před 5 měsíci +4

    instead of initializing the whole buffer with zeros you can set the last char of it to zero:
    #define BUF_LEN 256;
    char buffer[BUF_LEN];
    buffer[BUF_LEN - 1] = '\0';
    thanks for your content!

    • @eitantal726
      @eitantal726 Před 5 měsíci

      Then your buffer is filled with garbage (former stack data) except the last char. You merely ensure that if an un-initialized char buffer is used, it will stop at the end at least. I wouldn't recommend this technique unless CPU time is so critical that "whatever = {0};" is too expensive

    • @meni181818
      @meni181818 Před 5 měsíci

      @@eitantal726that is the main reason.

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      @@eitantal726 I agree with Eitan on this point, it is a good practice to always ensure that you are working with all zeroed buffers or else it can lead to weird bugs

  • @SuborbitalSentinel
    @SuborbitalSentinel Před 5 měsíci +3

    Do you have any videos on your dev setup? I'm interested in how you are running what looks like dwm and the windows terminal?

    • @nirlichtman
      @nirlichtman  Před 5 měsíci +1

      My welcome link in the channel description contains info about my setup, I also have a video about this on my channel

  • @Jonathan-ru9zl
    @Jonathan-ru9zl Před 3 měsíci +1

    Hi Nir. Thanks for the videos 🙌
    Do you recommend using Linux over Windows OS?

    • @nirlichtman
      @nirlichtman  Před 3 měsíci +1

      Whatever you find most comfortable, I like using Windows as my main OS and Linux through WSL

    • @Jonathan-ru9zl
      @Jonathan-ru9zl Před 3 měsíci +1

      @@nirlichtman I don't get it why people are still using Linux as their main OS (And saying that it is better than Windows)?
      Let me explain my point of view (Related to Ubuntu mostly):
      1. Linux OS is asking for the password every once in a while, approx. 100 times a day, wasting a bunch of time.
      If you're not lucky, Linux can't detect even the right password
      Isn't it archaic and stupid?
      2. Linux isn't compatible with many software programs and drivers
      3. It is a command base OS - you have endless shortcuts and commands which are impossible to remember
      4. End of life after couple of years, which requires you to install a new version
      5. GUI in Windows is extremely better
      I'm embedded developer, so I am using Linux only because I have to, but for my daily use, I'm happy with Windows

    • @nirlichtman
      @nirlichtman  Před 3 měsíci +1

      @@Jonathan-ru9zl Yah my main reasons for preferring Windows as my main OS is the compatilibilty and that I like the user interface and am very familiar with it, and now that there is WSL there is not really a reason for me to switch to Linux, but i like using Linux distros for server side since they mostly have low hardware requirements and are comfortable for those kind of stuff

  • @m0zart101
    @m0zart101 Před 5 měsíci +1

    Hi Nir, I watched most of your videos and I liked your projects very much. So I want to learn how do you improved yourself so much in low-level programming.

    • @nirlichtman
      @nirlichtman  Před 5 měsíci +3

      Thanks! What helped me the most with learning low level is to make my own projects and play around with various linux/windows api calls, for example one of my projects was a c++ web server that stored messages that i sent it, another thing that helped me was to contribute to open source projects which is a great way to learn how to work with a lot of existing code and to use the debugger effectively.

    • @Abhishek-pp8ck
      @Abhishek-pp8ck Před 5 měsíci

      ​@@nirlichtmanHello Nir sir, i also want to learn linux/C/Networking, where do i start? do you know any resources....?

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      @@Abhishek-pp8ck the best way to learn in my opinion is to make your own projects in c and work with the man pages alongside to learn about the os functions, for example an idea for a networking project in c can be a web server and client, or maybe an online simple game, for a general guide on networking i remember beejs guide to networking as good

  • @guilherme5094
    @guilherme5094 Před 5 měsíci

    👍👍Thanks!

  • @AK-vx4dy
    @AK-vx4dy Před 5 měsíci

    What are you using with this numerated tabs on top ?

  • @grimvian
    @grimvian Před 5 měsíci

    For a immediately C coder like me, it's a great topic to incorporate error handling the correct C way.
    And a big thanks for not using music and not using disturbing video editing.
    May I ask, why you kind of do advertising for a Chinese brand?

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      Are you referring to my hostname with "lenovo" in it? that is not part of any advertising on purpose, it's my laptops name for many years, but indeed it is a good idea now to change the name didn't think about this, thanks for the feedback :)

    • @grimvian
      @grimvian Před 5 měsíci

      @@nirlichtman Yes I am and just what i thought. Now retired reseller and had installed thousands of Windows computers, by wiped out everything on the hard drive. I just installed a clean system and minimum of drivers. Now it's Linux Mint LMDE and C programming with Code::Blocks.
      Keep up the great work.

  • @gblargg
    @gblargg Před 5 měsíci

    2:40 Set your command prompt to show when a command failed, and its code. Makes it easier to spot failed commands in interactive sessions.

    • @cyrilemeka6987
      @cyrilemeka6987 Před 5 měsíci

      How?

    • @gblargg
      @gblargg Před 5 měsíci

      @@cyrilemeka6987 See "Bash prompt with the last exit code" (assuming you use Bash).

  • @niki7968
    @niki7968 Před 5 měsíci

    I have a quick question. Why do you use the windows terminal, or at least riced some other terminal to look like the window's one, on linux? Is it just for the laughs?

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      I am using Windows as my main OS and a dwm port for Windows, more info about my setup in the channel description

  • @NecdetSanli
    @NecdetSanli Před 4 měsíci

    How did you split your terminal? Do you use vim as editor?

    • @nirlichtman
      @nirlichtman  Před 4 měsíci

      Yes I use Vim and I split using the built in windowing, for more info I have a video about cool window splitting features on Vim

  • @ayushmaanshrotriya3677
    @ayushmaanshrotriya3677 Před 5 měsíci

    Hey. Can you do a video on c based project like creating heap memory manager??

  • @its_code
    @its_code Před 5 měsíci +1

    ❤❤❤❤❤❤😊

  • @jochen_schueller
    @jochen_schueller Před 5 měsíci

    Yeah, very important, but can also be done later

  • @bigapple89
    @bigapple89 Před 5 měsíci

    Could you make a video on creating a simple email server? Thanks anyway!

  • @BSPNode
    @BSPNode Před 5 měsíci

    Before I get into this, shouldn’t main return an integer? Also I see you dereferencing the “strrchr” function, but I didn’t even know that was possible. Can anyone explain if this is valid C?

    • @Not_Even_Wrong
      @Not_Even_Wrong Před 5 měsíci +1

      You're correct. When I have time I'll compare the assembly and see what or IF the compiler does anything differently.
      This is why C is so insane... Why not just throw a 'this is wrong learn C' error? No the compiler just does 'something '.... Smh

    • @nirlichtman
      @nirlichtman  Před 5 měsíci +1

      Actually, returning void from main is completely valid C :)
      According to the documentation, since C99 the main entry point can return void and this means the program exit code is undefined devdocs.io/c/language/main_function
      About the deref of strchr, it also valid C, but as I explain later in the video, it is recommended to check if it failed before derefing it

    • @BSPNode
      @BSPNode Před 5 měsíci

      @@Not_Even_Wrong Fair enough, I'm mainly a Windows programmer, so seeing how "void main" is legal confused me a bit. After a minute of research, a lot of the C++ people hate the idea of "void main", and require programmers to use "int main" instead.

    • @BSPNode
      @BSPNode Před 5 měsíci

      ​@@nirlichtman Learn something new every day :) - I'm mainly a computer graphics programmer, so I don't know how I got here, but great video though!

    • @Not_Even_Wrong
      @Not_Even_Wrong Před 5 měsíci

      @@nirlichtman okay it's valid in the sense that unspecified in C means implementation defined. It can always return 0 or 1 or whater BUT the compiler has to decide. In contrast to undefined which means nobody has to decide... This is the part of C that I don't like... Thanks for replying either way. Taught me once again to not trust stackoverflow...

  • @nno64209
    @nno64209 Před 5 měsíci +1

    Can you make a window tutorial with assembly, please ?

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      You mean opening a window with Assembly? Or Windows the OS?

  • @ItsCOMMANDer_
    @ItsCOMMANDer_ Před 5 měsíci +1

    Another good example is yohttps srrver video, after the ser er has been used once (at leadt for me) bind() will fail the next time with return code - 1. I dobt want to blame you , this might be a problem with my wsl setup. But without debug printing the return values, i would have never kniwn that bind was the reason the server only works sometimes

    • @nirlichtman
      @nirlichtman  Před 5 měsíci +1

      Your comment on that vid was indeed one of my inspirations for making this video :) Bind indeed can fail sometimes if you run it again on the same port not long after the previous run has finished (my guess is that it is due to some OS used ports cleanup that has not happened yet)

    • @ItsCOMMANDer_
      @ItsCOMMANDer_ Před 5 měsíci

      most likely true, because errno returns 0.@@nirlichtman

  • @styx2084
    @styx2084 Před 5 měsíci

    do you use DWM in windows?

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      yes, more info on the welcome link on my channel description

  • @robertolin4568
    @robertolin4568 Před 5 měsíci

    Without error handlings, we live with the shell scripts.

  • @sbef
    @sbef Před 5 měsíci

    Instead of that printf that prints a numeric errno, you can just use perror.

  • @Benjaneb
    @Benjaneb Před 5 měsíci

    It looks like you're running dwm on Windows, how is that possible?

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      I am using a port for Windows, the welcome link on my channel desc contains additional info

  • @AlaskanInsights
    @AlaskanInsights Před 15 dny

    another good reason is the use of void main.... errrrr

  • @otaxhu8021
    @otaxhu8021 Před 5 měsíci

    I did not know about errno header file

  • @xBiggs
    @xBiggs Před 5 měsíci +2

    void main lul

  • @ryku112
    @ryku112 Před 5 měsíci

    hi how do you find the functions with man,when i try it it doesn't work

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      You can get the dev man pages with your package manager, if you are using Debian/Ubuntu you can run apt install manpages-dev, more info about my setup in the channel description link

    • @ryku112
      @ryku112 Před 5 měsíci

      @@nirlichtman tysm

  • @OlliS71
    @OlliS71 Před 5 měsíci +1

    Better use RAII and Exceptions (here system_error) with C++. Much less work.

  • @greyfade
    @greyfade Před 5 měsíci

    Why are you printing the errno with printf? Use `perror(3)`. It will print the error string to stderr (which is what you should be doing), so you don't have to use errno on the command line. This is better for users and better for logging.

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      Right, totally agree, that was a mistake

  • @AdventuresOfPepero
    @AdventuresOfPepero Před 5 měsíci

    wait! wait!
    you are inside windows?!
    how did you use dwm inside windows?

    • @nirlichtman
      @nirlichtman  Před 5 měsíci +1

      check out the welcome link in the channel description :)

  • @smylesg
    @smylesg Před 5 měsíci

    I guess I would start by checking s != -1. No point in building structures or making other calls unnecessarily.

  • @masterbedroom594
    @masterbedroom594 Před 5 měsíci +1

    Hi Nir, how are you? Are you safe?

    • @nirlichtman
      @nirlichtman  Před 5 měsíci +1

      My country is going through complicated times, but I am safe, thanks

    • @alister_
      @alister_ Před 5 měsíci

      @@nirlichtman Where are you from? :\ Wish you the best.

    • @nirlichtman
      @nirlichtman  Před 5 měsíci

      @@alister_ thanks, I am from israel

  • @LabiaLicker
    @LabiaLicker Před 5 měsíci

    ayo hold up. your seriously using the edge browser? 💀

  • @kamertonaudiophileplayer847
    @kamertonaudiophileplayer847 Před 5 měsíci

    Rust uses a similar error handling, but you can skip it, you have to explicitly say - ignore an error.

  • @DK1PL
    @DK1PL Před 5 měsíci

    The Importance of Error Handling in C: I have expected some ideas of sophisticated error handling in C and I found just a basic of the return value handling as on first page of the C language tutorial. 👎

  • @swedishpsychopath8795
    @swedishpsychopath8795 Před 5 měsíci +1

    FAKE TUTORIAL: At 2:27 if you stop the video and microstep back and forth (with dot and comma) you can spot the compile error he didn't want us to see.

    • @nirlichtman
      @nirlichtman  Před 5 měsíci +1

      Actually, the tutorial is completely real - that is a compiler warning, not an error (the warning is related to the address structure - this is not related to the point of the video anyway) and I am completely fine with you seeing it, it appears for more than a microstep no need for dot and comma.