Exception Handling - C++ Tutorial For Beginners #18

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

This video tutorial covers exception handling in C++, including try-catch blocks, throwing exceptions, and catching exceptions, with examples of dividing by zero and handling error codes.

Full Transcript

it's not a game it's [Music] a what is going on guys welcome back to the C++ tutorial Series in today's video we're going to talk about exception handling so let us get right into it so let's talk about the basic idea behind exception handling now let's say you have some piece of code here and this piece of code uh is prone to errors you can get some errors or exceptions here something can happen that you don't want to happen but you cannot necessarily say okay uh you cannot prevent it with ifs because whenever you can prevent something with ifs you want to prevent it with ifs but sometimes things happen during runtime that you cannot prevent with ifs or it's hard to prevent them with ifs or you don't want to do it um so in those cases what you want to do is you want you just want to run the code and in case something goes wrong you want to do something now in Python for those of you who come from a python background we have the try accept oh sorry try accept structure so we have a block where we try something and then in case it goes wrong we execute the accept block uh for those of you who come from a C background and you don't know any other programming languages which I don't think that anyone out there knows C and no other language but nevertheless if you come from a C background you know that there are error codes you don't know what try catches and you don't deal with exceptions because in C what we do is we have some piece of code for example I don't know printing something whatever and or actually printing something is probably the dumbest example that I could have come up with but let's say we have something like opening a semaphore or closing a semaphor whatever happens it can return an error code so uh let's say we try something St for something and if this something Returns the error code ne1 we go in here and we do FR printf you don't need to learn this this is just C uh and you have some error message that you send to SD error and so on blah blah blah whatever and then you go X exit failure like that that is the C way of doing things in C++ we don't do it like that we can still do it like that but you don't have to do it like that uh because in C++ we throw exceptions and we catch those exceptions so imagine we have a function up here called divide it takes two floats float one and Float two and the basic idea here is if F2 equals 0 because division by 0 is def find now C++ lets you do that let me just show you that it does um if I go ahead and say C out 10 / zero this will not give me any errors uh what happened here doesn't give me errors but it doesn't give me an output either I think if we do it like that float F1 float F2 C n fub1 CN FS2 and then we do fub1 divided by FS2 and I now input seven and zero we get infinite or Infinity as a result which mathematically speaking is not correct because the division by zero is undefined so this should not be Infinity necessarily unless you define it to be Infinity right but uh let's say we want to create a function that does not allow a division by zero what we could do is we could say okay if the second parameter here if the second number is zero throw an exception so we use throw the keyword throw then we can specify an error code here so let's say we specify 15 for whatever reason and uh now what we can do here is we can say we're not going to use the division here we're going to use uh the divide function and we're going to pass F1 and F2 uh we of course need to say what happens else otherwise we're going to return uh fub1 / FS2 and if we now run this I can go ahead and say 7 / 4 and we get 1.75 I can say 8 / 2 and we get four and now I can try to say 9 / by 0 and you can see terminate called after throwing an instance of int so we got an exception here so we can do it that way we can either write our own functions that throw exceptions but even if you don't want to to do that there are libraries out there that just throw exceptions when something goes wrong for example when you try to open a file that doesn't exist when you try to to do stuff that you should not be doing or when stuff happens that should not be happening um and what we can do instead of letting the program just crashed because now you you know whenever you get an exception the program crashes and if you don't want this to happen you can use a try catch structure so you say try try this piece of code and if something goes wrong if we get an exception catch this exception um and in here we just have to specify what kind of exception it is in e in this case and then we're just going to say C error which is like C out so if you don't use STD uh you should be writing stdc error don't forget that best practice and then we say uh error for example like that so now if I try to divide by zero we get error now of course in this case the program also terminates but if we had some code down here like uh see out test or something like that it would still run that piece of code and we would get this if we wouldn't catch this so if we remove the try catch structure here we would not see the last line of code so if I say 7 / by 0 it terminates immediately when the exception is thrown so with try catch we can catch the exception uh which essentially means that we have taken care of it and we can proceed with the rest of the code now if you want to um distinguish the individual error codes you can go ahead and say if E equals 15 for example we can say see out division by Z is undefined and otherwise we can just print a general error message like that so in this case if we have something like six divid by 0 we would get division zero is undefined if we get some kind of other exception here we could also uh or we would still print error a generic error message so that's it for today's video hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you very much for watching see you in the next video and bye [Music]

Original Description

Today we talk about exception handling in C++. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches exception handling in C++, including how to throw and catch exceptions, and how to handle error codes, with a focus on dividing by zero as an example.

Key Takeaways
  1. Define a function that throws an exception
  2. Use a try-catch block to catch the exception
  3. Handle the exception by printing an error message
  4. Distinguish between different error codes
💡 Exception handling is crucial in C++ programming to prevent program crashes and handle errors gracefully

Related Reads

📰
The no-code AI business stack that earns $5k/month on autopilot
Learn how to build a no-code AI business stack that earns $5k/month on autopilot, leveraging interconnected AI tools for content creation, customer payment processing, and more
Dev.to AI
📰
GitHub’s April Changelog Exposes the Private-Repo Cost of Instant Reviews
GitHub's April changelog reveals that private-repo reviews now consume AI credits and Actions minutes, affecting automatic review rules
Medium · AI
📰
AI Did Not Kill Freelancing. It Changed What Clients Actually Pay For
Discover how AI has changed the freelancing landscape and what clients now pay for, to stay ahead in the industry
Dev.to · Alcora
📰
25 Best AI Tools in 2026 to Boost Productivity, Create Content & Make Money Online
Discover 25 AI tools to boost productivity, create content, and make money online in 2026
Medium · Startup
Up next
How I Use AI to Write Facebook Ad Scripts That Actually Scale
Nick Theriot
Watch →