I used to see try and catch in other peoples' code and I would ignore it because I had absolutely no idea what it did. You did a good job of explaining it quick and simple.
Jens Donlin An exception is an exceptional (out of the ordinary) event that requires special handling or change in the flow of programming. Although nullPointerErrors are strangely not so exceptional... wiki.haskell.org/Error_vs._Exception en.wikipedia.org/wiki/Exception_handling
Jens Donlin actually you there two types of error compile time error which can be corrected . There are something called runtime error other than exception which cannot be resolved
Errors should not, but can be be handled; they are unchecked "exceptions" that represent extreme conditions and will typically cause the app to fail. It's like this: Exception and Error classes extend Throwable. Checked Exceptions extend directly from Exception (eg. InterruptedException) and happen at compile time, whereas unchecked Exceptions extend RuntimeException (RE included, which extends Exception) and happen at runtime. Checked exceptions should be handled with a catch block, whereas unchecked exceptions & errors should not be handled although they can be.
Some information on what people mean when saying "Exception" vs. "Error": An exception will occur when something would otherwise cause a program to break (think dividing by 0), but is handled. An error is when an exception occurs, but is not handled, which causes your running program to fail. V helpful vid. +1
@@Supaship6000 yes I noticed that :D (old is gold) Thank you. Hopefully you're at the point in your life where I wish to be right now. But anyways I'll enjoy the journey for now.
@@shibrahmisbah4760 yeah i imagine I must have been in school the time I initially wrote this comment. This road has been very good to me. Since then I've gotten heavily involved in Web development (especially the Angular community).
I cannot believe how easy it is. Something that I was not able to learn in 10 hours, I learnt it in 9 minutes. You sir, are the best, thank you very much.
All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the exception class hierarchy. Immediately below Throwable are two subclasses that partition exceptions into two distinct branches. One branch is headed by Exception. The other branch is topped by Error.
ive been reading exception handling all day from my textbook. I understood it, but didn't really grasp it until now. I think a lot of people have their "aha!" moments watching your videos. Thanks!
Instead of a do-while loop, a much simpler way is to put it in a while loop that says while(true) so it is infinite. Then if the user makes it to the end of the loop with no errors, just add the break; statement and it will break out of the loop. Either way works though.
Why can't my teacher explain exception handling like this...seriously it becomes so simple when i hear it here. same thing with enums Keep up the good work
Great video and nice explanation. Only problem is exceptions and errors are not the same thing. Although both are sub classes of the Throwable class they are branched off to be subtly different. Apart from that great job on making a nice clear tutorial yet again!
Damn you saved me! Going to have test in university tomorrow, and untill now I didn't understand Exeception Handling. This video helped me alot! Thanks
My professor is god awful so I left the lecture early, figured since I watched your tutorials back in high school it would be easy to learn from you. I was right.
Dude, okay, i've watched quite a few of your videos. You're good; possibly the best at creating these. I can't stand getting indians that are spending half of there tutorial stuttering over the english language.
hey what if you input a character/string/decimal in place of an int? I tried to see if it can catch the exception but "something" happens. The program seems to loop endlessly without taking an input. Why does this happen?
My Eclipse don´t consider the division by zero as Exception, rather Infinity. When I implement ArithmeticException in catch block I still recieve Infinity! what is the solution here ?
Can you do it with string? I have a project on GUI it's a number guessing game from 1-50 but i have to use Exception on these two: 1. number that go beyond or below the number. And 2. Any string input. I got the number 1 but I'm struggling at number 2. Any help?
If I were a CS professor I would pretty much just make my lectures 1 to 1 copies of these tutorials and make the rest of the class a lab to work on student projects. People would probably learn a lot more that way.
I have a question, how can you make it so that it handles exceptions with letters AND repeats the program? I can get it to do those tasks separately, but I can't seem to get it to do both at the same time.
I still don't understand the main reasoning of using the do while loop in this code and why x is assigned to 2 specifically? Can someone please clarify? Thank you!
How do I tell the user the specific errors instead of lumping everything as "You can't do that"? eg. if he inputs 0 for n2, how do I tell him that he has entered an invalid value of zero for n2? Or if he entered too large a number, how do I tell him that the number is too large, etc?
you can do it like boolean control = true; while(control) { sysout("Enter Number"); int x = input.nextInt(); if(x == 0) sysout("it cant be 0,type different number"); else n2 = x; boolean control = false; }
How would I accept "enter" (return) as a valid way to end the loop after I ask the user to enter a series of numbers? In other words..."Enter your numbers, then press enter when all numbers have been accepted." So...Again, instead of x==2, I need x==enter? Do you get what i'm saying? How do I do that :)
Nick Wilson There is no need to catch the enter key. The user presses enter for each number put in. It just starts after the last one. Or, if you let the user input the numbers separated by spaces, you would still read that as one line of input which is terminated by pressing enter or something equivalent, automatically. The "x==2" is pretty weird in this example. Bucky could just use a boolean, set it to false in front of the loop, then set it to true once input was correct. I am not sure why he doesn't do that. Booleans should be common knowledge for viewers.
in this series of Java, video chapter 80,81 includes expection handling amd later on video 82 teaches whats an exception handling is... I would suggest to correct the order of the videos amd update the series.. Infact overall the series is Awesome... Hatsoff .
hey there.. i am trying to catch an exception when i register a user and he types in a username that already exists(username is a primary key on my database) so it comes up with an error of duplicate PK. I tried to catch the error when the user types in the username he wants at the preparedstatement.getText of the username field.. but it does not work still.. any ideas? ^^
Quick question how would I change the values of n1 and n2 in the catch block would it be as simple as going n1 = 2 n2 =1 ? We're assuming that I'm doing another question that requires an n1 and n2
in case of someone put a string instead of an int we need to clean the memory of the filled case (n1 and n2) by adding the line "input.nextInt();" inside the : catch(Exception e) { System.out.println("You cant do that"); input.nextInt(); } and Bam !! lol now we can read only integer .. ;) and thnx thenewboston
I've already taken an intro to programming class but this video and these videos will probably help me out a lot I'm going to subscribe for at least add this to my favorites thank you
what if the program i wrote into the try and catch is a while-loop and i still want to retry the loop if there is a exception in it? greetings from 2021
Code goes into infinite loop if we give a double as an input to any of the numbers! Any suggestions how to fix that part of code and that program also correct errors that includes dviding with 0?
e is an object of the Exception class. You are declaring e as an object of the Exception class ex: catch(Exception e){}. You could rename e to anything you wanted.
Its been 3 years, I don't know if you even used this account, I don't even know if you're still alive. Anyways, Imma ask. In what ways would you use "e"? or why do we gotta have it in there?
Junior e is an object of the class Exception, you are able to utilize the class methods such as getMessage and getStackTrace to pull out more info on the exception, it's useful for custom error logging to another file or displaying error message to console etc
Why am I getting an infinite Loop? public static void main(String[] args) { Scanner input = new Scanner(System.in); int x = 1; do { try { System.out.println("Enter the first number"); int n1 = input.nextInt(); System.out.println("Enter the second number"); int n2 = input.nextInt(); int sum = n1 + n2; System.out.println(sum); x = 2; } catch (Exception e) { System.out.println("You can't do that!"); } } while (x == 1); }
Interesting use of an integer variable as a condition to completing the try block. A boolean would've been nice to see instead of an integer, but hey, nice to mix things up here and there.
Better way to do loops like that: reference:While(1==1){ ... System.out.println("Type X to close application: "); switch(input.nextLine()){ case X:{ break reference;} }
this is because when you try to divide an int value / int value,the result is always an int value in most of the programming languages. When you divided 2/3, the probable answer would be 0.6666666.... But what actually happened was that all the digits after the decimal were ignored, and only 0 was considered as the result. if you want the answer as 0.666667, try this- float x=2.0f, y=3.0f; float res=x/y; System.out.println(res); or try with "double" if you want the output to be more accurate.
For some reason my do/while loop becomes infinite even though i'm increasing by integer by 1 at the end of the try code. Anybody know what the problem may be??
funny we have online courses at work (which I take for refreshers) and I its really hard to understand, and then I turn to Bucky and I'm like "and its that simple?" LOL
How can I resolve situation: when is num1=38 , num2=sdf I know that num2 is not int, but when some stupid user input something like that, that will be much bigger problem try,You can see!
I laughed pretty much throughout the entire video. This guy reminds me of the time my high school teacher forced us to make a powerpoint presentation to the class.
Congratulations !! Just one question my dear friend !! And if the user input a String instead of Int ? What would be the treatment in this case ?? and if this code is inside a repetition structure, where do I put the break?
i learned more from u than my professor lol
tell me about it
+Shifra Sirotsky She learned more from Bucky than from his professor
+Brock Jaeger she* :)
hi!
Amen
these decade old videos are the best lol. they age like fine wine
I used to see try and catch in other peoples' code and I would ignore it because I had absolutely no idea what it did. You did a good job of explaining it quick and simple.
Error and Exception are different :
Error -> Can't be handled
Exception -> Can be handled
vinay ganesh.d
correct me if Im wrong, but an error happens at compile time, while an exception happens at run time.
Jens Donlin An exception is an exceptional (out of the ordinary) event that requires special handling or change in the flow of programming. Although nullPointerErrors are strangely not so exceptional...
wiki.haskell.org/Error_vs._Exception
en.wikipedia.org/wiki/Exception_handling
Jens Donlin actually you there two types of error compile time error which can be corrected . There are something called runtime error other than exception which cannot be resolved
Errors should not, but can be be handled; they are unchecked "exceptions" that represent extreme conditions and will typically cause the app to fail. It's like this: Exception and Error classes extend Throwable. Checked Exceptions extend directly from Exception (eg. InterruptedException) and happen at compile time, whereas unchecked Exceptions extend RuntimeException (RE included, which extends Exception) and happen at runtime. Checked exceptions should be handled with a catch block, whereas unchecked exceptions & errors should not be handled although they can be.
I meant Exception And Errors are two different categories. Exception handling is possible and errors not possible
Some information on what people mean when saying "Exception" vs. "Error":
An exception will occur when something would otherwise cause a program to break (think dividing by 0), but is handled. An error is when an exception occurs, but is not handled, which causes your running program to fail.
V helpful vid. +1
thanks zack! preparing for my midterm exam here.
@@shibrahmisbah4760 that comment was from 6 years ago lol but still glad it was helpful. Best of luck with your midterms!!
@@Supaship6000 yes I noticed that :D (old is gold) Thank you. Hopefully you're at the point in your life where I wish to be right now. But anyways I'll enjoy the journey for now.
@@shibrahmisbah4760 yeah i imagine I must have been in school the time I initially wrote this comment. This road has been very good to me. Since then I've gotten heavily involved in Web development (especially the Angular community).
"some idiot tried to divide by 0" :')
Stefan Dobre Summary of the 2008 Financial Crisis.
Very Funny :-)
Only guy above can accent that
@Stefan Dobre story of my life
I can't believe how easy this actually is. Thanks man
I searched so hard for Exception handing vids but I still had some questions after tuning in, but this one just cleared all the crap... Thank you
I cannot believe how easy it is. Something that I was not able to learn in 10 hours, I learnt it in 9 minutes. You sir, are the best, thank you very much.
Is there a throw tutorial somewhere in here?
czcams.com/video/UHRU973uu2c/video.html
@@purplepidge hahahahaha didnt expect that you caught me off guard (see what i did there )
"Some idiot tried to divide by 0" LOOOOL
sir, this video from like 11 years ago actually did helped me so much. Thank you very much
All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the
top of the exception class hierarchy. Immediately below Throwable are two subclasses that
partition exceptions into two distinct branches. One branch is headed by Exception. The other branch is topped by Error.
You managed to condense a forty minute lecture video I previously watched into ten minutes, and make it actually easy to understand. Thank you....
ive been reading exception handling all day from my textbook. I understood it, but didn't really grasp it until now. I think a lot of people have their "aha!" moments watching your videos. Thanks!
gotta catch em' all!
It took millions of years for mankind to evolve
Nice explanation I'm still watch in 2019
me too bro
@@SirFency I watch in 2020 He Is Great
@@hossamnasser9717 2021
Want more teacher's like u.. greatly explained..
Instead of a do-while loop, a much simpler way is to put it in a while loop that says while(true) so it is infinite. Then if the user makes it to the end of the loop with no errors, just add the break; statement and it will break out of the loop. Either way works though.
Wow. I've never seen anyone explain this concept in a manner so simple yet concise. Thank you so much Bucky (:
The way you explain things is very clear and efficient. Thank you for all of your tutorials
I had a two hour lecture on exceptions this week and this video has made it so much clearer to me. Thank you
bucky explained loops in 4mins, where some people take a 3 hour class
Why can't my teacher explain exception handling like this...seriously it becomes so simple when i hear it here. same thing with enums
Keep up the good work
Great video and nice explanation. Only problem is exceptions and errors are not the same thing. Although both are sub classes of the Throwable class they are branched off to be subtly different.
Apart from that great job on making a nice clear tutorial yet again!
Julian Gurung Yea I was thinking the same thing. Error is something which cannot be fixed by programmer or it is outside his scope.
Your channel is a god-send for my college Java class. Super clear and concise 5/7
Thank you! Our course could have pointed to this video and avoided 3+ hrs of boring syntax reading. Well done on your presentation.
Damn you saved me! Going to have test in university tomorrow, and untill now I didn't understand Exeception Handling. This video helped me alot! Thanks
I love the way Bucky simplifies the concepts instead of throwing in a bunch of other concepts at the same time, the way Deitel and Deitel does.
Great explanation! thank you very much :)
+AlexandraJohnsson WTF ??? learn some kindness
My professor is god awful so I left the lecture early, figured since I watched your tutorials back in high school it would be easy to learn from you. I was right.
Dude, okay, i've watched quite a few of your videos. You're good; possibly the best at creating these. I can't stand getting indians that are spending half of there tutorial stuttering over the english language.
hey what if you input a character/string/decimal in place of an int? I tried to see if it can catch the exception but "something" happens. The program seems to loop endlessly without taking an input. Why does this happen?
I'm not kidding, this is by far the best example and very very good explanation for how it all works. As expected, old tutorial is better
THANKS SO MUCH! Man you have no idea how much you're saving my ass!
Wow, dude, this was really helpful. I figured you should use a while loop but the way you used the do-while loop was simple yet ingenious.
Thanks a lot. You made the Exception Handling easy to understand and it saved me a lot time if I learned that on my own.
My Eclipse don´t consider the division by zero as Exception, rather Infinity. When I implement ArithmeticException in catch block I still recieve Infinity! what is the solution here ?
Java never looked so easy.Great tutorials.
Can you do it with string? I have a project on GUI it's a number guessing game from 1-50 but i have to use Exception on these two: 1. number that go beyond or below the number. And 2. Any string input. I got the number 1 but I'm struggling at number 2. Any help?
If I were a CS professor I would pretty much just make my lectures 1 to 1 copies of these tutorials and make the rest of the class a lab to work on student projects. People would probably learn a lot more that way.
Preparing for my java exam, so thanks for a clear and precise explanation.
God speed
That was the simplest way to use a loop on a try catch thanks for the video my dud 👌
Awesome explanation, you made a confusing java subject much clearer.
it's been six years, how time flies
it's going to be a 9 years lol i hope you get a notification on my comment xD
I have a question, how can you make it so that it handles exceptions with letters AND repeats the program? I can get it to do those tasks separately, but I can't seem to get it to do both at the same time.
Thanks helped for a reminder before an exam.
Always come to thenewboston after class
I still don't understand the main reasoning of using the do while loop in this code and why x is assigned to 2 specifically? Can someone please clarify? Thank you!
Im a little confused, wouldnt you be able to accomplish the same thing by using a while loop that repeats the code if the user enters a 0?
How do I tell the user the specific errors instead of lumping everything as "You can't do that"?
eg. if he inputs 0 for n2, how do I tell him that he has entered an invalid value of zero for n2? Or if he entered too large a number, how do I tell him that the number is too large, etc?
you can do it like
boolean control = true;
while(control)
{
sysout("Enter Number");
int x = input.nextInt();
if(x == 0)
sysout("it cant be 0,type different number");
else
n2 = x;
boolean control = false;
}
Thank you for posting this video. What would be the Java code to write text into an existing word (.docx) file?
you are the best at explaining Java that I've found on CZcams :)
If you try to input alphabetic character why does the loop become infinite?
Try giving this program an input of: e or any alpha character
coming in clutch for finals. thank you so much man. i'm glad I found your channel.
How would I accept "enter" (return) as a valid way to end the loop after I ask the user to enter a series of numbers? In other words..."Enter your numbers, then press enter when all numbers have been accepted." So...Again, instead of x==2, I need x==enter? Do you get what i'm saying? How do I do that :)
Nick Wilson There is no need to catch the enter key. The user presses enter for each number put in. It just starts after the last one. Or, if you let the user input the numbers separated by spaces, you would still read that as one line of input which is terminated by pressing enter or something equivalent, automatically.
The "x==2" is pretty weird in this example. Bucky could just use a boolean, set it to false in front of the loop, then set it to true once input was correct. I am not sure why he doesn't do that. Booleans should be common knowledge for viewers.
I actually figured it out already and it was for a data structures assignment. So I had to have the user press enter to end the loop.
in this series of Java, video chapter 80,81 includes expection handling amd later on video 82 teaches whats an exception handling is... I would suggest to correct the order of the videos amd update the series..
Infact overall the series is Awesome... Hatsoff .
hey there.. i am trying to catch an exception when i register a user and he types in a username that already exists(username is a primary key on my database) so it comes up with an error of duplicate PK. I tried to catch the error when the user types in the username he wants at the preparedstatement.getText of the username field.. but it does not work still.. any ideas? ^^
Correct me if i'm wrong, but in the console you get an ArhitmeticException, So isnt there any special exception for that?
9 years and still better than college lecture
ikr, damn
true
this is the coolest tutorial that i ever watched in youtube
I know this video is old but you are a life saver bro!!!!
almost done with the whole series!!!!!!!!! YOU ROCK SIR... YOU ROCK!!
Midterm today , excellent video 9min video better than 50 min lecture :)
Quick question
how would I change the values of n1 and n2 in the catch block
would it be as simple as going n1 = 2 n2 =1 ?
We're assuming that I'm doing another question that requires an n1 and n2
I literally learned more from you then from school
in case of someone put a string instead of an int we need to clean the memory of the filled case (n1 and n2) by adding the line "input.nextInt();" inside the :
catch(Exception e) {
System.out.println("You cant do that");
input.nextInt(); }
and Bam !! lol now we can read only integer .. ;) and thnx thenewboston
I've already taken an intro to programming class but this video and these videos will probably help me out a lot I'm going to subscribe for at least add this to my favorites thank you
BRO YOU HELP ME TO UNDERSTAND WELL THIS TRY AND CATCH METHOD, RATHER THAN IN MY SCHOOL THNXX ALOT BRODIE !
what if the program i wrote into the try and catch is a while-loop and i still want to retry the loop if there is a exception in it? greetings from 2021
would not a simple while loop work as same? cause while x=1 its gonna run the code inside the while loop once.
Code goes into infinite loop if we give a double as an input to any of the numbers! Any suggestions how to fix that part of code and that program also correct errors that includes dviding with 0?
Fix? It's not broken. You cannot put doubles in ints. If you want to use doubles just change the variables to double.
e is an object of the Exception class. You are declaring e as an object of the Exception class ex: catch(Exception e){}. You could rename e to anything you wanted.
Its been 3 years, I don't know if you even used this account, I don't even know if you're still alive. Anyways, Imma ask. In what ways would you use "e"? or why do we gotta have it in there?
@@Junior-rz2gx I have the same question, did you figure it out?
@@Kal-EL_Volta I have no idea, we left to study another chapter, I just know that I gotta have it there.
Junior e is an object of the class Exception, you are able to utilize the class methods such as getMessage and getStackTrace to pull out more info on the exception, it's useful for custom error logging to another file or displaying error message to console etc
Why am I getting an infinite Loop?
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x = 1;
do {
try {
System.out.println("Enter the first number");
int n1 = input.nextInt();
System.out.println("Enter the second number");
int n2 = input.nextInt();
int sum = n1 + n2;
System.out.println(sum);
x = 2;
} catch (Exception e) {
System.out.println("You can't do that!");
}
} while (x == 1);
}
Add this to your catch area after the error message:
input.nextLine();
Jeremy Soronen Why does this work, and why does it happen? Happened to me to.
Prof. Killionare It's because the value of x will always be equal to one, as it was declared outside the do loop. Should be it.
Wait, it's supposed to be...an infinite loop. That's what he did up there.
you write x1+x2
make it x1/x2
Explained it very well, thank you!
Interesting use of an integer variable as a condition to completing the try block. A boolean would've been nice to see instead of an integer, but hey, nice to mix things up here and there.
Better way to do loops like that:
reference:While(1==1){
...
System.out.println("Type X to close application: ");
switch(input.nextLine()){
case X:{ break reference;}
}
When I try to implement this, the loop just continues printing "Enter the x value". Help pls
this is because when you try to divide an int value / int value,the result is always an int value in most of the programming languages.
When you divided 2/3, the probable answer would be 0.6666666....
But what actually happened was that all the digits after the decimal were ignored, and only 0 was considered as the result.
if you want the answer as 0.666667, try this-
float x=2.0f, y=3.0f;
float res=x/y;
System.out.println(res);
or try with "double" if you want the output to be more accurate.
Thanks for pure speaking about that lesson i understood all thank you so much you are the best teacher :) love u
R.I.P apple.java ;(
How do I differentiate the errors such as
2/0 = error 1
dsverf = error 2 (Because i input a string instead of int)
build more catch's from specified to general error
You need to add "return;" at the end of your catch statement. That way it knows to return to the start and try/catch again.
Bucky fixed a spelling error! :O
Man still now its one of the best .
You saved my life in Object programming class 😭
just making it so simple for us to understand thank you!!
For some reason my do/while loop becomes infinite even though i'm increasing by integer by 1 at the end of the try code. Anybody know what the problem may be??
funny we have online courses at work (which I take for refreshers) and I its really hard to understand, and then I turn to Bucky and I'm like "and its that simple?" LOL
Thanks thenewboston. A great explanation, just what I needed.
How can I resolve situation: when is num1=38 , num2=sdf
I know that num2 is not int, but when some stupid user input something like that, that will be much bigger problem
try,You can see!
2:45 hahahahahahahaha this guy is my favorite teacher.
lol, I laughed at that part, immediately checked comments if someone enjoyed this too. Also he continues his joke at 7:00
You're right he is the best teacher.
I laughed pretty much throughout the entire video. This guy reminds me of the time my high school teacher forced us to make a powerpoint presentation to the class.
Thanx! I have exam today, watching you tutorials for revision. So clear and understandable! =)
you are awesome :-)
Very much useful and simple explanation, thank you very much
BETTER THAN MOST UNIVERSITY LECTURERS
Congratulations !! Just one question my dear friend !! And if the user input a String instead of Int ? What would be the treatment in this case ?? and if this code is inside a repetition structure, where do I put the break?
great tutorial, opening a lot of good topics and give clues
i watch your video one day before the exam and pass every single fucking time.........so thank you
I followed the code. If I give the input as a string which is an alphabet it goes to an infinite loop. Can you explain how to handle this
I got the same thing as you said.
Um, I did the exercise here and instead of catching the exception the console output said: Infinity ????? wat.jpg