Тёмный

Can I Handle Exceptions with Try Catch in C? (setjmp, longjmp) 

Jacob Sorber
Подписаться 159 тыс.
Просмотров 31 тыс.
50% 1

Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.thinkific.com
Website ➤ www.jacobsorber.com
---
Is Try Catch Style Exception Handling Possible in C? // Inquiring minds want to know. Can you make try-catch blocks in C? Can you throw exceptions? Exceptions are used all over in other languages, but not in C. This video shows you how to use setjmp and longjmp (and a few preprocessor tricks) to implement your own exception handling in C.
Make Videos: • Learn make in 60 seconds.
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorber.com
people.cs.clemson.edu/~jsorber/
persist.cs.clemson.edu/
To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon --- [ / jacobsorber ]
Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]

Опубликовано:

 

3 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 136   
@junbird
@junbird 3 года назад
I had this "object oriented C" phase a couple of years ago, a bit after starting university. I tried to implement as many features from higher level languages that I could in C, so classes, inheritence, overriding, polymorphism, etc. It was terrible, but it was also a great learning experience that made me discover many details about the C language and kinda how OO languages work under the hood. I also tried to implement exceptions of course, and I did it basically how it's shown in the video, but using even more macro magic (which of course is not a good idea, as pointed out in the video) and using the return value of setjmp in order to return an address to an exception object (I even tested at runtime that it was an instance of the "Exception" class requested by one of the catch block). Not something I'd recommend anyone actually using (mainly because C is not meant to be used like that), but again, it was fun.
@rkkm67
@rkkm67 2 года назад
How did you learn to do all that? Is there a tutorial one can follow or any book?
@alec7987
@alec7987 Год назад
@@rkkm67 I did something like it as well. You do lots of Googling. I found all sorts of small posts all over the internet that helped me get it working. I even wrote a somewhat usable transpiler that can convert a vaguely C++ like class declaration into a header and source files that can be included.
@laughingvampire7555
@laughingvampire7555 Год назад
google for the "compiler design in c" book by Allen I Holub, is an old book, you can download the copy and shows you how to do just that in C. by the way, I wouldn't call OO higher level than C, is just an organization scheme of code around single dispatch, not higher level.
@ladyViviaen
@ladyViviaen 3 года назад
the versatility of c and how low level everything is really makes it fun to tinker with!
@JacobSorber
@JacobSorber 3 года назад
Definitely.
@paulk314
@paulk314 3 года назад
Are you familiar with the book "C Interfaces and Implementations"? One of the early chapters does what you did here, using the C preprocessor to implement try-catch-throw, but adds a stack to it allowing for multiple levels of exception handling, as well as some other niceties. Nice video!
@JacobSorber
@JacobSorber 3 года назад
Cool, I've seen the book, but apparently need to look more closely. Thanks.
@paulk314
@paulk314 3 года назад
@@user-io4sr7vg1v actually, yes. I wanted to read the whole thing but I only got a few chapters in and my brain needed a long break 🤣
@rallokkcaz
@rallokkcaz 2 года назад
It's chapter 4 to be exact, I'm reading it right now. Wish me luck haha.
@diceandbricks
@diceandbricks 3 года назад
One important issue to keep in mind is proper use of volatile keyword for any local var that's modified after the setjmp.
@dannyhunn6025
@dannyhunn6025 3 года назад
In college I was always taught to handle exceptions with goto, enum, a boiler plate, and 2 bit fields ( just two uint64_t). when an exception happens you set 2 bits in the bit fields ( one for location and one for the exception) then goto your exception handling area, each bit corresponds to an enum( for read abilty) in your boiler plate and loop through the bits with a a switch print where it happened and the exception. It also gives the chance to recover from the exception. Isn't setjmp a non local goto anyway?
@an0nsaiko890
@an0nsaiko890 3 года назад
I love C but sometimes find it somewhat lackluster, but these videos show some very neat features from other languages that you can emulate. Great stuff.
@brycemw
@brycemw 3 года назад
If you really wanted to do something crazy, you could have it so that when you “try”, you first allocate a temporary variable and use that to store the old jmp_buf before saving the new state. Then you restore it after. If you did it right then it should be possible to do nested tries without adding more globals. You would probably want to make the global thread-local as well. This is really interesting so I might try making my own version at some point for fun!
@doowi1182
@doowi1182 3 года назад
This channel is incredible. Keep up the good work!
@TheCocoaDaddy
@TheCocoaDaddy 3 года назад
Neat video!!!! I've never used setjmp and longjmp and I found your use of them clever to implement try/catch. :) Thanks for posting!
@unperrier5998
@unperrier5998 2 года назад
setjmp/longjmp a're typically used to restart a program. This is how a service restarts without losing its connections for instance or how configuration reload can be implemented.
@unperrier5998
@unperrier5998 2 года назад
The use of "exception_value" (global variable) can be avoided by using "switch/case" statements instead of "if/then".
@godnyx117
@godnyx117 3 года назад
Best programming channel in YT!!!! Thanks cannot express how much I appreciate what you do for us! I truly wish you the best!
@JacobSorber
@JacobSorber 3 года назад
Thanks! It's always good to hear that the channel is making a difference. Thanks for the support.
@user-jx8uz6tb6k
@user-jx8uz6tb6k 2 года назад
Thanks for existing!
@bluehornet6752
@bluehornet6752 3 года назад
Somewhere, right now, Bjarne Stroustrup is clutching his chest and is about to lose consciousness. That said, I thought it was a damned cool video!
@botbeamer
@botbeamer 3 года назад
xD
@HansBezemer
@HansBezemer 2 года назад
Nice going! I used that one before, but I learned something never the less!
@victorlacerda9659
@victorlacerda9659 3 года назад
This was one of most elucidative explanation that i had have. Thanks You very much !!! :)
@ls_hower
@ls_hower Год назад
Finally I've understood their usage, thx!
@joshermar
@joshermar 3 года назад
Awesome and enjoyable as usual! Please consider doing a video on goto statements and their potential uses. Cheers.
@rafalmichalski4893
@rafalmichalski4893 3 года назад
Great video Jacob !!! Once I did mechanism but based on single pass do-while loop and "break" (no goto at least) was acting as "throw" (more or less like "try" but without real "catch")
@AnubhavApurva
@AnubhavApurva 2 года назад
Thanks a lot good explanation!
@renhaoxue5220
@renhaoxue5220 2 года назад
Thanks! Good stuff
@theglowingone5643
@theglowingone5643 3 года назад
I remember trying to create exceptions in C a few months ago and I wish I would have seen this video before. I have currently another project but after that I want to try to implement asyncio (asynchronous IO) and I was wondering if one could do it using setcontext() and getcontext(). Can you make a video on that if you find the time? Thanks for your videos, they really keep me motivated to keep programming as I learn a lot from them.
@JacobSorber
@JacobSorber 3 года назад
You're welcome. Glad they're helpful. And, sure, I could probably do that. setcontext/getcontext and of course makecontext/swapcontext are a lot of fun. I'll see what I can put together.
@neerajkale
@neerajkale 3 года назад
This is awesome! Thank you.
@JacobSorber
@JacobSorber 3 года назад
You're very welcome.
@SaiMouli034
@SaiMouli034 17 дней назад
Nice explanation
@ashgoku6966
@ashgoku6966 3 года назад
My mind's blown . I never thought we could use preprocessed directives this way . Your channel never fails to suprise me , keep up the good work
@rdwells
@rdwells 3 года назад
One advantage of C++ exceptions is that they clean up objects as the stack unwinds, which longjmp can't do. Unfortunately, this also results in one of the disadvantages: the time it takes to handle an exception is (effectively) non-deterministic, so they shouldn't be used in hard real-time code. Still, they're one of the things that would make it really difficult for me to go back to use C (the other being the STL).
@astromec6303
@astromec6303 6 месяцев назад
Well this point of whether or not exceptions should be used can be argued. Because u will have to clean up resources yourself anyways without exceptions so it’s basically the same. And in real time critical applications it could actually be very important to do so as it may not necessarily behave the same way as typically. E.g. in an embedded system u may not necessarily get a segmentation fault or the resources may not actually be reclaimed after program exit like in a modern OS.
@OmarRampado87
@OmarRampado87 3 года назад
Like the goto statement, I use longjmp with care. To check the return of setjmp I use the switch statement and the case for each error. The errors are enum. Thank for sharing this interesting alternative!
@JacobSorber
@JacobSorber 3 года назад
You're welcome...and absolutely right about using care. Anytime you are jumping around, there's cause for concern. Opportunities to mess things up abound. 😀
@botbeamer
@botbeamer 3 года назад
Really interesting video
@iuppiterzeus9663
@iuppiterzeus9663 3 года назад
I'm in "the C world" for like half a year now and did not know this was a thing. Nice, thank you !
@AlexSmith-jj9ul
@AlexSmith-jj9ul Год назад
For what you are trying to do you don't even need to use setjmp or longjmp a function of the form: void throw(char * exception) { printf("Uncaught exception: %s ", exception);//or print to standard error whatever you perfer exit(1); return; } Will do just fine and exit properly.
@cernejr
@cernejr 3 года назад
BTW, the best piece of c++ code that I have worked on, a speech recognition engine developed over decades by very smart people, did not use exceptions, it just returned error codes when needed.
@JacobSorber
@JacobSorber 3 года назад
A great deal of exceptional code has been written without exception handling. Few things in programming are one-size-fits-all.
@Kotesu
@Kotesu 3 года назад
Yes, that sounds familiar. What you are describing was considered best practice by several of my mentors when I was starting in embedded systems. Typically, every function returned a result code which was an enumerated integer, the values were encoded (bitpacked) to have a module ID and error ID in a single number and 0 was reserved for success. That way in a deep cal stack you could tell which module was causing problems by merely returning any error code you received. I still prefer this over complicated try..catch blocks as it is simpler and more consistent with normal execution flow (no sudden jumps up the call stack when you have several nested blocks).
@Maklaka
@Maklaka 2 года назад
If you wanted to next these, you would need to add an array of jmp_bufs and more defines like END_CATCH and END_TRY to be used at the bottom of those scopes to decrement the index into it once you are done with it.
@szarinmehr
@szarinmehr 2 года назад
Hi Jacob I work for a company that has a huge code base of C and C++ generated over 40 years. We use the same mechanism to catch exceptions for both c and c++ in our implementation layer. This is indeed very smart. The only down-side of it in c++ is that longjump will not rewind the stack. For example, if you have a c++ std::vector object on the stack with hidden heap allocation its destructor will never be called and it will leak memory. I know you prefer c over c++ and I admire your reasons, but exception handling which may come with a tiny cost in c++ is where c++ really shines. Thanks for your wonderful tutorials!
@axalius572
@axalius572 3 года назад
no classes: structs and functions no exceptions: error codes no stl: write your own code simple
@TheCocoaDaddy
@TheCocoaDaddy 3 года назад
This was pretty much how I started my programming career! lol
@SirRebonack
@SirRebonack 3 года назад
no namespaces
@axalius572
@axalius572 3 года назад
@@SirRebonack Namespace_Function
@zofe
@zofe 3 года назад
Hiding the complexity by means of ignoring it, really?
@glumfish6862
@glumfish6862 3 года назад
@@zofe C doesn't hide any complexity. You have all the power in your hands, sometimes too much.
@bart2019
@bart2019 2 года назад
To make the try nestable your savebuf should be like a stack. After the try/catch blocks are executed, an item should be popped from the stack, or the next throw would put you back there - not only not doing the right thing, but possibly causing a crash.
@mow3890
@mow3890 3 года назад
Great 👌
@rishabhanand4270
@rishabhanand4270 3 года назад
How about the finally block? Great vid btw.
@tordjarv3802
@tordjarv3802 3 года назад
I think that this example really illustrates that try-catch style exception (also known as Structured Exeception Handling (SEH)) is just like goto, but way worse since it allows you to jump to a completely different part of the program and not just within the same method. Personally I prefer the style of exception handling that Rust uses, by exploiting monadic function calls where exception information is backed in to the return type and it is transparent that things could go wrong. But I do think that this was a really interesting video
@unperrier5998
@unperrier5998 2 года назад
A big warning that you could have hightlighted is that "savebuf" can not and must not be used outside of the function where it has been initialized (where we call setjmp) because of the black magic longjmp() does with the stack. In a nutshell, as soon as we exit the function that calls setjmp(), savebuf becomes invalid because it contains reference(s) to a stack frame that doesn't exist any longer. Experienced programmers can workaround this limitation by using a different yet compatible stack frame, but it's libC implementation and compiler dependent, hence dangerous.
@JacobSorber
@JacobSorber 2 года назад
Good point. Thanks for mentioning that.
@Fraket
@Fraket 3 года назад
Cool!
@peterSobieraj
@peterSobieraj 4 месяца назад
My first reaction was "this is crazy". But then at end of video I kind of get use to setjmp. I think I will stick with setjmp, and not create any defines for it. I like it. I think that for programmers "pretty" is just something that they are use to.
@anon_y_mousse
@anon_y_mousse 2 года назад
This is pretty close to how I handled the same thing, except I incorporated the current line number to allow multiple states with each nested level. Granted, it'll fail if some dingus puts everything on one line, but since I'm the only one using it it's okay. Though it was definitely easier than actually implementing setjmp and longjmp for my own libc. Now that I've been using RPi's for a while I may go back and add a version for ARM as well, perhaps revisit the idea of extending C just for fun and allow one liners that use it.
@MikkoRantalainen
@MikkoRantalainen Месяц назад
4:45 The only sane way to do this is to require pointer to jmp_buf as an argument to the function that wants to potentially "throw". That way you don't need function return site specific global variables to implement this. So instead of signature compute(avg, values, length) you would have signature compute(throw_buf, values, length).
@michaelkotthaus7120
@michaelkotthaus7120 3 года назад
According to the MISRA guidelines from 2004, unions shall not be used (Rule 18.4). In the 2012 MISRA edition, this became only advisory (rule 19.2). Personally, I try to avoid unions. But there are some situations (also mentioned here in the comments), where an union is the only or the best solution. Related to your example, Jacob, a C++ programmer would probably advice to use polymorphic classes. In C, a union could be avoided by using pointers to structs which contain the "big" data (personality, firmware). Only one of these pointer must be unequal to 0. With this, you could also save the boolean flag which distinguishes between robot and human beeing. Nevertheless: Another great episode which helps me to think about data concepts. Thank you!
@rogo7330
@rogo7330 5 месяцев назад
Nice thing about this is that setjump and longjump are defined in libc by POSIX and pretty much since C89 (that's what manpage said), so your code should work just fine across most of the compilers and systems. Just interesting how bad it is for CPU cache and compiler optimizations in general and what other caveats that will have, but this is like working with signal handlers (across all languages, by the way), pretty low level and needs you to understand what you can and should not do in them.
@eliavrad2845
@eliavrad2845 3 года назад
Is there an equivalent to Generators in c? It sounds like a neat and interesting way to refactor complicated, nested looping logic over simple functions: would it have problem like slowness? I thought of this because had to print results, and had repeated sections of for (...) { ...complex, nested looping logic for getting each element ...; fprintf("field1: %d", element->field1); } which I guess could be done with macros, but would be nice to refactor to some generator function for (init();done();next()) { fprintf("field1: %d", element->field1); }
@muzafferirmakyavuz9993
@muzafferirmakyavuz9993 3 года назад
Sir can u explain struct "constructor" using function pointers
@praenubilus1980
@praenubilus1980 3 года назад
Want to see a discussion between exception handling vs error code. Cannot find some good discussion over the internet.
@ic6406
@ic6406 7 месяцев назад
Some info about internal state would be fine to add into the video. For example, does this jump pop stack correctly, in other words, is stack pointer back to its position prior to the call to this average function
@islandcave8738
@islandcave8738 3 года назад
You could use this to simulate generators with yield.
@weydans
@weydans 3 года назад
Hi from Brazil! Some videos like it just make me ask: is there some thing impossible to implement on C Language? Congratulations for the amazing content!
@JacobSorber
@JacobSorber 3 года назад
Thanks! The theoretical answer is nearly always, yes you could do most anything in C, given enough time and effort. But, that time and effort component can be significant. 😀
@Alche_mist
@Alche_mist 3 года назад
There always is the halting problem...
@kuaiyu977
@kuaiyu977 3 года назад
I think python's try/catch will handle crashes like an out of bounds array index. Will setjump/longjump also handle that? If not it just feels like a wrapped goto statement.
@patrick_jane2164
@patrick_jane2164 2 года назад
Nice video and a cool idea! but you used an explicit throw statement, what happens if you divided for example? without a throw clause? Will it catch the exception?
@maxaafbackname5562
@maxaafbackname5562 Год назад
No, these are standard library functions, not a language construct.
@marusdod3685
@marusdod3685 3 года назад
woah didn't know this
@Sayan_Shankhari
@Sayan_Shankhari Месяц назад
8:36 just define or use enums for different different possible error types
@diceandbricks
@diceandbricks 3 года назад
How would this work with multi threads? savebuff couldn't be global. Can you pass savebuff or *savebuff down the call stack as a parameter?
@gordonsau2989
@gordonsau2989 3 года назад
After watching the video, I would like to know how often people do something like this. Is this how setjmp and longjmp are commonly used? Are the other languages with try catch blocks use something like setjmp and longjmp which save the stack pointer and other registers to implement the exception features?
@unperrier5998
@unperrier5998 2 года назад
setjmp/longjmp a're typically used to restart a program. Call setjmp() first thing in main() This is how a service restarts without losing its connections, or how configuration reload can be implemented. Some errors like invalid instruction can also be handled by "soft restarting" the program this way. Amazingly, it works great.
@AshtonWolfwood
@AshtonWolfwood 9 месяцев назад
To make this thread safe, do we need to pass the ’savebuf’ as parameter to the function?
@MithicSpirit
@MithicSpirit 2 года назад
You could also have used switch case
@IIARROWS
@IIARROWS 2 года назад
I'm amused and terrified at the same time.
@gabrielcasanova7034
@gabrielcasanova7034 Год назад
is this the perfect use case for goto labels?
@ShotgunLlama
@ShotgunLlama 2 года назад
Will setjmp always save the execution context to whatever is passed to it, discarding anything already in that jmp_buf? In other words, as shown here, this wouldn't work for nested try/catch blocks, would it?
@TomStorey96
@TomStorey96 2 года назад
I believe he said you could nest if you used different jmp_bufs.
@sudarshanv9797
@sudarshanv9797 3 года назад
For all windows users, if you want to try C in linux, try windows subsystem for linux (version 2), a subsystem available in windows, there are lot of videos and documentations about linux in windows, I am using it, I am having fun with linux along with windows
@FelipeMendez
@FelipeMendez 3 года назад
Does this work across libs? if I compile an exe and a dll does and the dll "throws" the exe catches it?
@JacobSorber
@JacobSorber 3 года назад
I haven't tried this on Windows. So, if by "DLL" you mean windows DLL, then I'm not sure. Try it out and let me know. If you're on a UNIX-based OS then yes you can jump from a library function into the calling process, as long as the library has access to the jmp_buf.
@ailijic
@ailijic 3 года назад
On most processors setjmp/longjmp just saves and restores all the registers. Would it work, yup. Will it do what you expect, maybe, probably not. You really want to limit the scope of setjmp/longjmp, e.g. use it in a library but return an error code to the consumer of the library. Setjmp/longjmp does nothing to manage memory.
@malyarenko.m
@malyarenko.m 3 года назад
But you haven't mentioned what happens to the stack frame of the function in which longjump was called. Is longjump going to pop the frame off the stack or some kind of memory leak will occure?
@wilfreddv
@wilfreddv 3 года назад
According to the man page longjmp transfers control to where setjmp was called, and restores the stack to the state it was in at the point where setjmp was called. Good eye for detail 🙂
@JacobSorber
@JacobSorber 3 года назад
Indeed. Good question. Yes, stack state and registers are restored.
@ailijic
@ailijic 3 года назад
It’s still C, you have to do your own memory management. If you are not careful you can have memory leaks, multiple initialization, double frees, and pointers pointing to bad data.
@taragnor
@taragnor 3 года назад
@@ailijic : Yeah, unlike C++ exceptions, it won't call destructors on objects when they go out of scope. You have to remember to do everything manually, which really does not scale well when you start to get into large projects.
@sababugs1125
@sababugs1125 3 года назад
Why not just use if else blocks ?
@Starwort
@Starwort 3 года назад
Why is this done with if-else instead of switch? Switch results in the (IMO slightly nicer): // jump_buffer can be removed and hardcoded if desired but this set of defines can be used for any code #define TRY(jump_buffer) switch(setjmp(jump_buffer)){case 0: #define CATCH(type) break; case type: #define CATCHALL break; default: #define ENDTRY } #define THROW(jump_buffer, type) longjmp(jump_buffer, type) TRY(savebuf) { // Code } CATCH(ENUMERATED_EXCEPTION) { // code } CATCHALL { // Generic } ENDTRY
@brayancodes
@brayancodes 3 года назад
That's why I love C! If you don't have something funny that other languages do, do it yourself xD. Great video. I learn a lot with your videos :)
@JacobSorber
@JacobSorber 3 года назад
Thanks. I'm glad you enjoyed it.
@helenmachelen4200
@helenmachelen4200 4 месяца назад
Doesn't seem to catch system exceptions (null pointer etc)
@GeorgeTsiros
@GeorgeTsiros 6 месяцев назад
i had almost forgotten about setjmp... dear god, no, why, why would you do this... is it even supported in long mode?
@unchayndspersonalaccount7690
@unchayndspersonalaccount7690 3 года назад
He starts talking about setjmp and longjmp at 3:02
@cernejr
@cernejr 3 года назад
setjmp, longjmp - that is how c++ implements exceptions, doesn't it? I am not an expert.
@JacobSorber
@JacobSorber 3 года назад
I haven't looked under the hood with C++ to see if they actually call setjmp/longjmp, but yes they are using the same sort of mechanism.
@ailijic
@ailijic 3 года назад
It calls into the c++ run time but basically it is like setjmp/longjmp. You can attach a debugger to a c++ program and see what’s it doing, not for the faint of heart. The c++ compiler keeps track of all of the objects and calls their destructors, in reverse order, as it jumps up the stack.
@nanda_8
@nanda_8 2 года назад
I might sound stupid here... I have one question... Why not create a global variable itself (instead of setjmp, longjmp) and make it 1 when the condition satisfies(i.e exception) EDIT: I got it.
@JacobSorber
@JacobSorber 2 года назад
Not a stupid question at all, but I'm glad you figured it out. This topic and it's interesting control flow issues can take a little time to wrap our heads around.
@aaaowski7048
@aaaowski7048 Год назад
btw, you cant catch a segfault. thats because its the OS that throws that. if i have been taught correctly, that is
@islandcave8738
@islandcave8738 3 года назад
Couldn't you have used static, instead of a global variable?
@embeddor2230
@embeddor2230 3 года назад
Can't believe how crappy this is, and yet so exciting :D Wonder if this is more effecient than normal exception handling from C++.
@mayankpant1596
@mayankpant1596 2 года назад
As a person who has been using Java almost all the time, the concept of not handling exceptions is so foreign to me that it took me years to learn c just because of that..
@wChris_
@wChris_ 3 года назад
this doesnt handle x86 exceptions! there is a Microsoft try-except extension you can use tho
@alantao3810
@alantao3810 3 года назад
😍😍😍😍
@alex_8704
@alex_8704 2 года назад
This is a very interesting and informative video, especially on pre-processing huckery. But try/catch is appealling when we want to catch the runtime errors, not just some conditioning in our algorithm. :)
@viacheslav1392
@viacheslav1392 2 года назад
exceptions in other languages are the result of conditioning... Conditioning happens at runtime...
@alex_8704
@alex_8704 2 года назад
@@viacheslav1392 Fair enough
@benjaminshinar9509
@benjaminshinar9509 3 года назад
love the video, hate the function.
@JacobSorber
@JacobSorber 3 года назад
That's fair. Do you have a preferred alternative?
@edgeeffect
@edgeeffect 2 года назад
2:25 "...any other language that's not Ruby, Python Java or pretty much any language that's not C" ... or Rust!! ;) It's one of the things that's always made me think exceptions are "hokey"... While, Until, Case, etc can all be emulated by simple IFs and GOTOs... and once they're in object code that's exactly what they are... but exceptions and exception handling???? ... PEP-20: "If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it MAY be a good idea." The fact that this isn't as "pretty" as a real try-catch block is just revealing the truth that exceptions are a damned ugly way of handling errors. In PHP-land we use exceptions to handle control logic as well as exceptional circumstances and it makes me sick-up in my mouth a little bit whenever I have to do it. Speaking as someone who doesn't really like C that much, I would say it's lack of exceptions is actually one of it's huge PLUS points.
@casperes0912
@casperes0912 2 года назад
This feels a tiny bit like a goto. Here it at least maintains the structure of the program but it feels like it could easily fall into the same problems the goto has. Though again in this case it doesn't seem to break the integrity of structured programming
@debasmitroy1694
@debasmitroy1694 3 года назад
Kind of ... try catch 😐
@ahmadhadwan
@ahmadhadwan 3 года назад
Thats pretty much what happens under the hood with high level languages, it just checks for the exception and jump to a specified address
@yimokkh3984
@yimokkh3984 Год назад
Нихуя не понял, можно на русском?
@KangJangkrik
@KangJangkrik 3 года назад
Try this: while(true) { fork() } Your PC will start dancing
@solsticeprojekt1937
@solsticeprojekt1937 11 месяцев назад
this has nothing to do with exception handling. at all.
@abdullahamrsobh
@abdullahamrsobh 3 года назад
before watching the video, let me guess, this would probably use goto
@googavo1d
@googavo1d 3 года назад
maybe some call/ret manipulations since it ends up at the setjmp just before it returns and changes return value to >0 so that cmp eax,0 would result in false
@khankashani7387
@khankashani7387 3 года назад
Professor we need c++, all jobs require C++, c is not worth of learning. I don’t see a single company that is asking to learn C or use it at work. Majority is using C++, C#, Java. If you really want to help us, the students we really need to master C++ with data structures and algorithms for jobs and interviews and problem solving. C is almost dead ☠️. It’s all C++ we need, please make C++ videos!!!!
@vespert0
@vespert0 2 года назад
You need to pull your head out of the sand or some cavity. C is still widely used in many areas, such as embedded computing, automotive, drivers, any application where speed is paramout, etc. Just because you don't see the need for it doesn't mean there isn't one. Also, don't be naive: the majority of those "C++" jobs are advertised as "C/C++" which 99% of the time means you'll be supporting legacy "C with classes" code.
@happygofishing
@happygofishing 3 дня назад
Phull sapart saar
@laughingvampire7555
@laughingvampire7555 Год назад
no classes, no exceptions, no stl, and you call that "limitations" dude, those are the FEATURES of C. C++ is a stinky pile of unnecessary complexity. embrace error code handling!! exceptions to be useful require static analysis (maybe integrated in the type system) to notify you of uncaught exceptions, are you implementing that? otherwise Exceptions will be a source of pain and maintainability for any piece of software you make. And they are inferior to the condition/restart system of Common Lisp and Smalltalk.
Далее
Making variables atomic in C
11:12
Просмотров 35 тыс.
Неожиданно?
00:25
Просмотров 77 тыс.
Make Up with Balloons 💄☺️🍓
00:23
Просмотров 1,9 млн
The Inline Keyword in C.
16:18
Просмотров 56 тыс.
CONCURRENCY IS NOT WHAT YOU THINK
16:59
Просмотров 88 тыс.
You Can't Unit Test C, Right?
23:09
Просмотров 51 тыс.
Fixed and Variable Length Arrays in C and C++
20:24
Просмотров 43 тыс.
C++ vs Rust: which is faster?
21:15
Просмотров 379 тыс.
Master Pointers in C:  10X Your C Coding!
14:12
Просмотров 285 тыс.