Error Handling: try and catch | JavaScript Tutorial in Hindi #60
Aug 15, 2023
Error Handling: try and catch | JavaScript Tutorial in Hindi #60
Link to the Repl - https://replit.com/@codewithharry/60t … Join Replit - https://join.replit.com/CodeWithHarry Download Notes - https://drive.google.com/drive/folder … Ultimate JS Course Playlist: • JavaScript Tutorials for Beginners in… ►Checkout my English channel here: / @programmingwithharry ►Instagram: www.instagram.com/codewithharry python, C, C++, Java, JavaScript and Other Cheetsheets [++]: Playlist: • Coding CheatSheets 🧾 by CodeWithHarry ►Learn in One Video[++]: Python[15 Hr]: • Python Tutorial For Beginners In Hind… Python Advance[3.5 Hr]: • Python Programming Course in Hindi (A… Python[1 Hr]: • Learn Python In Hindi In One Video - … Python[2 Hr]: • Python Tutorial In Hindi 🔥 Python[15 Min]: • 15 Minute Python Tutorial For Beginne… JavaScript[1 Hr]: • JavaScript Tutorial C[1.3 Hr]- • C Programming Tutorial For Beginners:… php[1 Hr]: • Learn Php In One Video In Hindi - हिं… php[2.3 Hr]: • Php Tutorial for Beginners in Hindi w… php[Project]- • Login And Registration Form Using Php… HTML[30 Min]: • HTML 5 Tutorial For Beginners In Hind… CSS[8.5 Hr]: • CSS Tutorial In Hindi (With Notes) 🔥 CSS[1.4 Hr]: • CSS 3 Tutorial For Beginners: Learn C… Wordpress[3.2 Hr]: • How To Make a WordPress Website | Wor… Angular[2 Hr]: • Angular Tutorial in Hindi Java[2.3 Hr]: • Java tutorial in hindi 🔥 Web Scraping[1 Hr]: • Web Scraping Tutorial using Python an… MongoDB[2 Hr]: • MongoDb Tutorial For Beginners in Hin… Numpy[1 Hr]: • Numpy Tutorial in Hindi Android Dev[12 Hr]- • Android Development Tutorial For Begi… Linux[1 Hr]: • Linux Tutorial For Beginners in Hindi JQuery[1.1 Hr]: • jQuery Tutorial For Beginners In Hind… Git and GitHub[1.1 Hr]: • Git \u0026 GitHub Tutorial For Beginners I… ►Complete course [playlist]: React: • React Js Tutorials in Hindi Python- • Python Tutorials For Absolute Beginne… OOP Python- • Object Oriented Programming Tutorials… Java: • Java Tutorials For Beginners In Hindi JavaScript- • JavaScript Tutorials In Hindi PHP- • PHP Tutorials in Hindi C- • C Language Tutorials In Hindi C++- • C++ Tutorials In Hindi Git \u0026 GitHub- • Git and GitHub Tutorials In Hindi Android Dev- • Android Development Tutorials in Hindi Python GUI- • Python GUI: Tkinter Tutorial In Hindi… Web Development- • Web Development Tutorials For Beginne… Python Django: • Python Django Tutorials In Hindi Projects Using HTML, CSS \u0026 Javascript- • Projects Using HTML, CSS \u0026 JavaScript… Data Structure and Algo: • Data Structures and Algorithms Course… Follow Me On Social Media ►Website (created using Django Rest \u0026 Angular): https://www.codewithharry.com ►Facebook: https://www.facebook.com/CodeWithHarry ►Instagram: https://www.instagram.com/codewithharry/ Twitter: https://twitter.com/CodeWithHarry Comment “#HarryBhai” if you read this 😉😉
Content
0.12 -> Guys, in today's video, we'll see
how to handle errors in Javascript.
4.4 -> We'll see "try catch"
syntax and learn how
8.085 -> all these things work. Let's get to
our computer screen and get started.
18.335 -> So, let us continue our journey. Today
we'll look at how we can deal with errors.
24.749 -> You people truly loved the
topic when we explored promises.
28.254 -> I'll always offer you guys
something amusing. But
30.811 -> today, I'll impart knowledge
to you, about try catch.
33.608 -> And I'll show you what will happen
if you get an error; Assume
39.409 -> that here, you would
like to put console.log ;
45.183 -> And there you type
"Hacking wifi....";
50.824 -> And you want to put "Please wait".
55.578 -> And then you want to put
61.826 -> "Fetching username and password";
68.347 -> Let's put this into setTimeout.
77.566 -> It's not giving suggestions though.
83.923 -> No! I just want to take this only.
88.237 -> After 1 sec what'll I do, ; Let
me put a 1000 milliseconds
92.867 -> After 1 sec I'll put "Hacking
wifi....Please wait...".
95.949 -> And after this, I'll replicate
it and type "Fetching
99.186 -> username and password", which'll
be performed after 2 seconds. Got it?
105.927 -> I'll do it after 2 seconds.
108.692 -> And in 3 seconds, I'll do something
else, for which let me replicate this.
113.653 -> No worries for that reason.
In 3 seconds;
118.167 -> Let's say my friend
Rahul is sitting beside me,
121.342 -> And I type "Hacking Rahul's
facebook id".
127.377 -> And for Rahul to freak out, I'll type
"User id and password of Rahul fetched".
135.803 -> Just to make Rahul so
uncomfortable, I'll type Rahul's No. too.
140.586 -> Please don't call this number. I'm
just randomly putting the numbers here.
147.737 -> So Assume it's Rahul's no. and it fetched.
151.678 -> Now let's run this console so and tell
Rahul that I'm hacking his computer.
157.55 -> He'll say Why are you joking? I'll
tell him see yourself.
160.433 -> Hacking wifi, fetching
username, hacking facebook,
164.394 -> mobile no. fetched and
Rahul just freaks out.
168.197 -> And he'll definitely change his
password on his way home. OK!
174.343 -> It'll be a funny thing to do.
Let's say I type console.log(rahul)
180.84 -> I don't know how your mind
became chemically fragile at this point;
184.25 -> Surest thing I know is that you've typed
console.log(rahul), so when you reload it,
191.612 -> You'll receive an
error, which is relevant,
195.732 -> your script will not proceed and our
hacking process has begun to implode;
199.495 -> But we also get an error here. So,
204.027 -> It's obvious that if this is written here,
the other set timeouts can easily work.
208.403 -> But what if you put console.log
here? So will the others still run? They
212.1 -> can't because the script execution
will start from the upperpart.
215.5 -> Only your first set timeout will run and
218.477 -> when the error appears
the others won't run.
222.529 -> The error comes straight
forward. Will you like that?
227.593 -> Do you realise what just arrived? Your
229.723 -> script has stopped,
and the rest can't run.
232.709 -> But you want that since your remaining
code must run even if you make a mistake.
237.488 -> Since you won't make blunders
like that in real-world situations,
242.069 -> Let's say you want to fetch
something from the network,
247.251 -> and you performed an error there;
249.881 -> You couldn't fetch the data or
a fire occurred in the servers.
254.8 -> Or let's say, out of jealousy, your
neighbour cuts your electricity wire.
258.681 -> Whatever happened, it failed to
fulfill your request and if this occurs;
263.772 -> Then you'll get an error. But when an
error appears, you still want to continue.
269.46 -> The syntax for such a task is
"try catch". You have to give it a try.
274.339 -> First, see if it runs in any way.
278.821 -> And after trying, if you
still don't get Rahul running;
285.656 -> Then don't throw an error. you are
saying to launch a console. log(error)
289.84 -> You are saying don't stop the script.
We should not get an error in the script.
293.875 -> Instead, while handling this, I
needed that error into the console.
298.253 -> do you underestand? It
means that if I reload it,
302.793 -> What I see is not an actual
error but a console.log statement.
307.19 -> I handled the error. Whenever
error comes, perform this.
311.033 -> Let's say when an error comes, I
might type "balle balle," my choice
314.823 -> If an error doesn't come, then it's
good,but when it does, type "balle balle".
318.847 -> So "balle balle" will appear and
other things will run without an issue.
323.95 -> If you want your script to
not stop because of your error,
327.24 -> then you can use "try catch".
Now let's move on to our notes
334.09 -> and inside Error handling I have written,
336.819 -> "We all make mistakes. Also
sometimes our script can have
340.428 -> errors. Usually a program
halts when an error occurs."
344.173 -> We know that already.
346.018 -> "The try catch syntax allows
us to catch errors so that the
349.57 -> script instead of dying can do
something more reasonable." Ok?
354.158 -> So if I hadn't chosen
to try to catch this, the
357.81 -> script would have stopped right here.
361.883 -> But with try catch, it still gives an
error,
366.883 -> but it will run with no issues.
371.833 -> Ok? It's that simple.
374.201 -> So try to catch syntax in such a way that
I told you. It also has two main blocks:
379.308 -> try and then catch. You also
get an error. You can also,
382.526 -> In catch, you can type "err"
or "error". Here, you can also
386.38 -> Let's say you type the
name of your crush, "Swati."
390.037 -> and then you'll get an error with the name
"Swati". I won't recommend such a thing.
394.782 -> What if people start typing Swati
everywhere? Please don't do that.
400.29 -> I was just giving an example. "Reference
error Rahul is not defined"appears here.
410.15 -> Now let's focus, " It works like
this. First the code in try is executed."
416.416 -> The code will be on trial
and if an error comes,
420.135 -> We'll try to ensure that the error
does not occur. First we'll do that.
424.3 -> The catch will be ignored. We'll
always try to ignore the catch.
430.803 -> We'll always try to not performan
catch, but when try has an error,
436.387 -> The catch will run and
it would have the control.
439.198 -> Wherever the error was
made, Try would halt right away.
442.531 -> And simply the code inside catch will run.
446.985 -> Try catch works synchronously. If an
exception happens in scheduled code,
452.8 -> I mean when exception appears in the
454.833 -> scheduled code, tri
catch will not handle it.
457.587 -> An example will help
you better understand
461.762 -> and you'll get a better
taste of what I mean.
464.518 -> Let's say I type console.log(rahul),
468.519 -> I have put this in try so it'll be
handled and it won't cause an error.
474.612 -> Just because I printed
this error, I get to
477.133 -> see it. And if I don't
perform +err as well,
480.193 -> Then also, I won't face a problem I'll
484.041 -> get "balle balle", "hacking wifi", etc.
488.721 -> But, here if we put the
same code into set timeout,
494.039 -> let's say "set timeout"
and then arrow function;
499.516 -> and I say that run it in
100 ms, almost immediately.
504.923 -> Ok! And if I you;
509.487 -> If you wish that
513.442 -> your code runs,
517.949 -> and handled if it has an error;
522.377 -> "Rahul is not defined".
Now you'll say that,
527.266 -> We put that into try, but still
the error was not handled.
530.441 -> Only the synchronous
code will be handled in try.
534.443 -> You have scheduled
this code at very least.
537.983 -> Now after scheduling, it's not
try-catch's guarantee that it'll work.
542.02 -> You have to type try catch
in it for that. If you do so;
546.627 -> If you type like this,
548.212 -> Then it will be handled too,
but it will be a different thing.
552.826 -> But if you write try-catchoutside
of the set timeout,
556.683 -> It won't be handled there. This
handles well, but this will not handle.
561.177 -> If I put try-catch here and not inside it,
565.52 -> So Javascript does not
take responsibility for that.
569.51 -> It'll be like, "Hey, I scheduled it
for you, man. Be happy with that. "
573.196 -> Now if someone asks you to drop their
son at the station and you did drop him;
579.713 -> Then she tells you that her son is having
582.977 -> bad habits; he won't
study. What is he doing?
586.637 -> You'll say I dropped him
off at the station. What
589.142 -> do I have to do with
what's going on in his life?
591.647 -> OK! So it's job is to simply
schedule things in try.
595.463 -> If the schedule doesn't happen,
then an error may occur. But,
599.142 -> when it schedules and when
the set timeout runs in the future;
603.868 -> I mean, even 100 ms
isfuture too. So when it runs
607.739 -> this is not try-catch's responsibility,
same is written in the notes as well.
612.01 -> "try catch works synchronously.
If an exception happens in
615.005 -> scheduled code like in set timeout,
then try..catch won't catch it."
618.525 -> It's so simple. You may have
developed an understanding of it.
622.601 -> try, set timeout, the
error containing code,
627.417 -> script dies and catch won't work.
630.349 -> "Thats because the function
itself is executed later, when
633.349 -> the engine has already
lefy the try...catch construct."
636.2 -> Do you get it? It means
that the javascript engine
638.744 -> told try to find an error in try;
643.212 -> If no error then schedule it.
645.823 -> This works like this, ok.
647.244 -> And then the scheduled
code, the time it'll run;
653.795 -> Then we'll get an error. This try
stopped looking for errors back then.
657.955 -> If something happens after that then,
660.236 -> It's like someone putting milk
on the stove and asking you
664.159 -> to keep an eye on it to
make sure it doesn't spill.
668.174 -> After ten years, you get a call
from the person who asked you
670.514 -> to keep an eye on the milk.
Today, that person put milk on
672.817 -> the stove again, but it spilled
because you weren't looking.
675.139 -> But you only did it once and
stopped doing it ten years ago.
677.6 -> So I hope that you understand all of this.
679.824 -> If you haven't accessed the
JavaScript playlist, then please
682.861 -> access it; we'll learn about
error objects in the next video.
686 -> Finally, we'll go over the clause
then questions and exercises.
690.31 -> So much is left for this course,
so kindly bookmark it and save it.
694.711 -> Many people claim that
it can not be found on
697.476 -> YouTube by searching
for "javascript Hindi tutorial."
701.151 -> Rather than searching
for it directly on YouTube,
705.519 -> you should access it from the channel.
It won't appear on search for some time.
710.422 -> That's all for this video guys,
Thankyou so much for watching it.
714.016 -> And I'll see you next time.....
Source: https://www.youtube.com/watch?v=WRNBQCl_cPU