Exception Handling - C++ Tutorial For Beginners #18
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
Related Reads
📰
📰
📰
📰
The no-code AI business stack that earns $5k/month on autopilot
Dev.to AI
GitHub’s April Changelog Exposes the Private-Repo Cost of Instant Reviews
Medium · AI
AI Did Not Kill Freelancing. It Changed What Clients Actually Pay For
Dev.to · Alcora
25 Best AI Tools in 2026 to Boost Productivity, Create Content & Make Money Online
Medium · Startup
🎓
Tutor Explanation
DeepCamp AI