Gotta Catch 'Em All? Why Pokemon Exception Handling Will Destroy You

Does your code crash the moment a user does something unexpected? In this lesson, Professor Leo and the AI team (Tulip & Daisy) teach you how to handle runtime chaos with elegance! We aren't just covering basic try-catch blocks. We’re diving into the "Crime Scene" of your code to explain why throw ex destroys your stack trace (and why Daisy calls it "hiding the body"). Plus, we'll settle the debate: is catching System.Exception, just "Pokemon Exception Handling"? (Gotta catch 'em all... or not?) 🦋 In this C# Tutorial, you will learn: ✅ The "Safety Bubble": How try-catch actually protects your users ✅ The using Statement: Why manual finally blocks are "Old School" thinking ✅ CRITICAL TIP: The difference between throw vs throw ex (Don't lose your stack trace!) ✅ Exception Filters: Using the when keyword like a nightclub bouncer for your errors Timestamps: 0:00 - Intro: C# Supremacy vs. Java Elegance 0:55 - The Crash: What is an Exception? 1:35 - "Pokémon" Exception Handling (Don't catch 'em all!) 2:30 - The using Statement vs. finally 3:15 - Exception Filters (when keyword) 3:35 - The Crime Scene: throw vs. throw ex 4:15 - Summary & Outro #csharp #dotnet #programminghumor #gamedev #professorleo

Transcript

Hello class and welcome back. I'm Hello class and welcome back. I'm Professor Leo. Today we aren't just Professor Leo. Today we aren't just Professor Leo. Today we aren't just writing code. We are preventing writing code. We are preventing writing code. We are preventing disasters. disasters. disasters. Uh we are talking about exception Uh we are talking about exception Uh we are talking about exception handling in C.

handling in C. handling in C. Finally, a topic that separates the pros Finally, a topic that separates the pros Finally, a topic that separates the pros from the people who let their apps crash from the people who let their apps crash from the people who let their apps crash and burn. C saves the day again. and burn. C saves the day again. and burn. C saves the day again. Calm down, sis. It's just try catch. We Calm down, sis. It's just try catch. We Calm down, sis. It's just try catch. We have that in Java, too, you know. And have that in Java, too, you know. And have that in Java, too, you know. And honestly, we do it with a bit more honestly, we do it with a bit more honestly, we do it with a bit more elegance.

elegance. elegance. Elegance, please. You probably write 10 Elegance, please. You probably write 10 Elegance, please. You probably write 10 lines of boilerplate just to catch a lines of boilerplate just to catch a lines of boilerplate just to catch a cold. Today is about Cup supremacy. cold. Today is about Cup supremacy. cold. Today is about Cup supremacy. #code and strawberries. #code and strawberries. #code and strawberries. All right, settle down two. We have a All right, settle down two. We have a All right, settle down two. We have a lot to cover. We need to teach the lot to cover. We need to teach the lot to cover. We need to teach the students how to stop their programs from students how to stop their programs from

students how to stop their programs from crashing when things go wrong, like when crashing when things go wrong, like when crashing when things go wrong, like when null pointer maximus attacks. So, what null pointer maximus attacks. So, what null pointer maximus attacks. So, what is an exception in C? It's a response to is an exception in C? It's a response to is an exception in C? It's a response to a runtime anomaly. If we don't handle a runtime anomaly. If we don't handle a runtime anomaly. If we don't handle it, the program stops executing it, the program stops executing it, the program stops executing gracefully and just crashes.

gracefully and just crashes. gracefully and just crashes. Like Tulip's mood when we run out of Like Tulip's mood when we run out of Like Tulip's mood when we run out of Strawberry Shortcake. Strawberry Shortcake. Strawberry Shortcake. Hey, that was one time. Anyway, Leo is Hey, that was one time. Anyway, Leo is Hey, that was one time. Anyway, Leo is right. Ideally, we want to wrap risky right. Ideally, we want to wrap risky right. Ideally, we want to wrap risky code in a security bubble so the user code in a security bubble so the user code in a security bubble so the user doesn't see a scary error message.

doesn't see a scary error message. doesn't see a scary error message. The most basic tool we have is a try The most basic tool we have is a try The most basic tool we have is a try catch block. catch block. catch block. It's super simple. You put the code that It's super simple. You put the code that It's super simple. You put the code that might explode inside the try block. If might explode inside the try block. If might explode inside the try block. If it throws an exception, the try block it throws an exception, the try block it throws an exception, the try block stops immediately and the code jumps stops immediately and the code jumps stops immediately and the code jumps right into the catch block.

right into the catch block. right into the catch block. Exactly. And Daisy, can we just catch Exactly. And Daisy, can we just catch Exactly. And Daisy, can we just catch anything? anything? anything? You could, but you shouldn't be lazy. You could, but you shouldn't be lazy. You could, but you shouldn't be lazy. You should use specific exceptions. If You should use specific exceptions. If You should use specific exceptions. If you just catch system exception, you're you just catch system exception, you're you just catch system exception, you're doing Pokemon exception handling.

doing Pokemon exception handling. doing Pokemon exception handling. Got to catch them all. Got to catch them all. Got to catch them all. No, you don't want to catch them all. No, you don't want to catch them all. No, you don't want to catch them all. You don't want to catch an outofmemory You don't want to catch an outofmemory You don't want to catch an outofmemory exception with a butterfly net tulip. exception with a butterfly net tulip.

exception with a butterfly net tulip. Right. If we catch generic exceptions Right. If we catch generic exceptions Right. If we catch generic exceptions without logging or rethrowing them, we without logging or rethrowing them, we without logging or rethrowing them, we might swallow bugs, making them might swallow bugs, making them might swallow bugs, making them impossible to find later. impossible to find later. impossible to find later. Exactly. Don't just sweep errors under Exactly. Don't just sweep errors under Exactly. Don't just sweep errors under the rug. If you catch null pointerous the rug. If you catch null pointerous the rug. If you catch null pointerous Maximus, aka a null reference exception,

Maximus, aka a null reference exception, Maximus, aka a null reference exception, you'd better have a plan to defeat him. you'd better have a plan to defeat him. you'd better have a plan to defeat him. Now what if we open a file in our try Now what if we open a file in our try Now what if we open a file in our try block but the program crashes before we block but the program crashes before we block but the program crashes before we close it.

close it. close it. Then you have a memory leak and that is Then you have a memory leak and that is Then you have a memory leak and that is so embarrassing. so embarrassing. so embarrassing. That's why we use the finally block. It That's why we use the finally block. It That's why we use the finally block. It executes no matter what whether the code executes no matter what whether the code executes no matter what whether the code crashes or works. The finally block runs crashes or works. The finally block runs crashes or works. The finally block runs less to clean up the mess.

less to clean up the mess. less to clean up the mess. Actually, professor, that's the old Actually, professor, that's the old Actually, professor, that's the old school way. Oh no, here comes the Java school way. Oh no, here comes the Java school way. Oh no, here comes the Java lecture. No, it's cleaner. Just wrap lecture. No, it's cleaner. Just wrap lecture. No, it's cleaner. Just wrap around a using statement. It makes the around a using statement. It makes the around a using statement. It makes the try finally work automatically in one try finally work automatically in one try finally work automatically in one line. See, less typing. Same safety.

line. See, less typing. Same safety. line. See, less typing. Same safety. Excellent point, Daisy. The using Excellent point, Daisy. The using Excellent point, Daisy. The using keyword is synthetic sugar that ensures keyword is synthetic sugar that ensures keyword is synthetic sugar that ensures your final block happens automatically. your final block happens automatically. your final block happens automatically. Show off. Just because she saved four Show off. Just because she saved four Show off. Just because she saved four lines of code doesn't mean Java is lines of code doesn't mean Java is lines of code doesn't mean Java is better.

better. better. Let's get advanced. C# has a cool Let's get advanced. C# has a cool Let's get advanced. C# has a cool feature called exception filters. feature called exception filters. feature called exception filters. Oh, I love these. You can add a when Oh, I love these. You can add a when Oh, I love these. You can add a when keyword to your catch block. It acts keyword to your catch block. It acts keyword to your catch block. It acts like a bouncer at a club. You only catch like a bouncer at a club. You only catch like a bouncer at a club. You only catch the exception if a certain condition is the exception if a certain condition is the exception if a certain condition is true.

true. true. For example, we could catch an exception For example, we could catch an exception For example, we could catch an exception only if the error code matches a only if the error code matches a only if the error code matches a specific number. And finally, what if we specific number. And finally, what if we specific number. And finally, what if we catch an error but realize we can't fix catch an error but realize we can't fix catch an error but realize we can't fix it?

it? it? Then you throw it. You can throw a new, Then you throw it. You can throw a new, Then you throw it. You can throw a new, more specific exception or just rethrow more specific exception or just rethrow more specific exception or just rethrow the original one so someone else up the the original one so someone else up the the original one so someone else up the chain can handle it. Whoa, stop. Don't chain can handle it. Whoa, stop. Don't chain can handle it. Whoa, stop. Don't write the throw like that. Why? I'm write the throw like that. Why? I'm write the throw like that. Why? I'm throwing variable X. You just wiped the throwing variable X. You just wiped the throwing variable X. You just wiped the fingerprints off the crime scene. If you

fingerprints off the crime scene. If you fingerprints off the crime scene. If you write throw X, you lose the original write throw X, you lose the original write throw X, you lose the original stack trace. Just write like this to stack trace. Just write like this to stack trace. Just write like this to keep the evidence. keep the evidence. keep the evidence. Exactly. We can't solve the murder if Exactly. We can't solve the murder if Exactly. We can't solve the murder if you hide the body. Tulip, you hide the body. Tulip, you hide the body. Tulip, I plead the fifth. I mean the finally.

I plead the fifth. I mean the finally. I plead the fifth. I mean the finally. To wrap up, remember exception handling To wrap up, remember exception handling To wrap up, remember exception handling keeps your application stable. Use try keeps your application stable. Use try keeps your application stable. Use try for risky code. Use catch for specific for risky code. Use catch for specific for risky code. Use catch for specific errors. Use finally to clean up errors. Use finally to clean up errors. Use finally to clean up resources. And never use empty catch resources. And never use empty catch resources. And never use empty catch blocks just to silence errors.

blocks just to silence errors. blocks just to silence errors. If you followed these rules, your code If you followed these rules, your code If you followed these rules, your code might actually be as stable as my Java might actually be as stable as my Java might actually be as stable as my Java architecture. architecture. architecture. Maybe Maybe Maybe in your dream sis, C is the best. in your dream sis, C is the best.

in your dream sis, C is the best. Good work, you two. Class dismissed. Good work, you two. Class dismissed. Good work, you two. Class dismissed. Thanks for watching everyone. Don't Thanks for watching everyone. Don't Thanks for watching everyone. Don't forget to like and subscribe for more forget to like and subscribe for more forget to like and subscribe for more coding adventures against nou pointerous coding adventures against nou pointerous coding adventures against nou pointerous maximus.

Watch on YouTube ← All Videos