Java Programming Tutorial - 82 - Exception Handling

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • Source Code: github.com/the...
    Core Deployment Guide (AWS): docs.google.co...

Komentáře • 648

  • @robinsparkles
    @robinsparkles Před 9 lety +531

    i learned more from u than my professor lol

  • @denizdemir9255
    @denizdemir9255 Před 3 lety +18

    these decade old videos are the best lol. they age like fine wine

  • @JordanShackelford
    @JordanShackelford Před 9 lety +12

    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.

  • @Dragosknight
    @Dragosknight Před 9 lety +249

    Error and Exception are different :
    Error -> Can't be handled
    Exception -> Can be handled

    • @jensjdon
      @jensjdon Před 9 lety +14

      vinay ganesh.d
      correct me if Im wrong, but an error happens at compile time, while an exception happens at run time.

    • @stormbot28
      @stormbot28 Před 9 lety +2

      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

    • @Dragosknight
      @Dragosknight Před 9 lety +2

      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

    • @kimgysen10
      @kimgysen10 Před 8 lety +1

      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.

    • @Dragosknight
      @Dragosknight Před 8 lety

      I meant Exception And Errors are two different categories. Exception handling is possible and errors not possible

  • @Supaship6000
    @Supaship6000 Před 10 lety +38

    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

    • @shibrahmisbah4760
      @shibrahmisbah4760 Před 3 lety +1

      thanks zack! preparing for my midterm exam here.

    • @Supaship6000
      @Supaship6000 Před 3 lety +1

      @@shibrahmisbah4760 that comment was from 6 years ago lol but still glad it was helpful. Best of luck with your midterms!!

    • @shibrahmisbah4760
      @shibrahmisbah4760 Před 3 lety +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.

    • @Supaship6000
      @Supaship6000 Před 3 lety +1

      @@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).

  • @stefan_dobre
    @stefan_dobre Před 9 lety +234

    "some idiot tried to divide by 0" :')

  • @mikeveranderen663
    @mikeveranderen663 Před 9 lety +12

    I can't believe how easy this actually is. Thanks man

  • @tendymaumela1292
    @tendymaumela1292 Před 9 lety +7

    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

  • @MrSevsbeasts
    @MrSevsbeasts Před 9 lety +1

    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.

  • @MrInsaneMixer
    @MrInsaneMixer Před 10 lety +79

    Is there a throw tutorial somewhere in here?

    • @purplepidge
      @purplepidge Před 4 lety +11

      czcams.com/video/UHRU973uu2c/video.html

    • @randomperson4321
      @randomperson4321 Před 3 lety

      @@purplepidge hahahahaha didnt expect that you caught me off guard (see what i did there )

  • @thetech3624
    @thetech3624 Před 4 lety +33

    "Some idiot tried to divide by 0" LOOOOL

  • @nurhayatiaimanshaifulakmam1454

    sir, this video from like 11 years ago actually did helped me so much. Thank you very much

  • @hiteshchalise3988
    @hiteshchalise3988 Před 7 lety +2

    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.

  • @dl3307
    @dl3307 Před 9 lety

    You managed to condense a forty minute lecture video I previously watched into ten minutes, and make it actually easy to understand. Thank you....

  • @farhanmandani
    @farhanmandani Před 10 lety

    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!

  • @josephsaidogabriel7740
    @josephsaidogabriel7740 Před 10 lety +102

    gotta catch em' all!

  • @mrgarg9664
    @mrgarg9664 Před 5 lety +15

    Nice explanation I'm still watch in 2019

  • @hiteshshibag1902
    @hiteshshibag1902 Před rokem +2

    Want more teacher's like u.. greatly explained..

  • @zayd3803
    @zayd3803 Před 4 lety +1

    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.

  • @MaxFangX
    @MaxFangX Před 11 lety +2

    Wow. I've never seen anyone explain this concept in a manner so simple yet concise. Thank you so much Bucky (:

  • @misschantalbelle
    @misschantalbelle Před 5 lety

    The way you explain things is very clear and efficient. Thank you for all of your tutorials

  • @keeblin
    @keeblin Před 9 lety

    I had a two hour lecture on exceptions this week and this video has made it so much clearer to me. Thank you

  • @Orcband
    @Orcband Před 7 lety +10

    bucky explained loops in 4mins, where some people take a 3 hour class

  • @MrSputtel
    @MrSputtel Před 12 lety

    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

  • @juliangurung
    @juliangurung Před 9 lety +1

    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!

    • @EP_1990
      @EP_1990 Před 9 lety

      Julian Gurung Yea I was thinking the same thing. Error is something which cannot be fixed by programmer or it is outside his scope.

  • @allyson8743
    @allyson8743 Před 8 lety

    Your channel is a god-send for my college Java class. Super clear and concise 5/7

  • @robertwagner8511
    @robertwagner8511 Před 6 lety

    Thank you! Our course could have pointed to this video and avoided 3+ hrs of boring syntax reading. Well done on your presentation.

  • @J0N36O
    @J0N36O Před 8 lety +1

    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

  • @martywme
    @martywme Před 13 lety

    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.

  • @AliAlHajji
    @AliAlHajji Před 9 lety +20

    Great explanation! thank you very much :)

  • @gmjammin4367
    @gmjammin4367 Před 4 lety

    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.

  • @m4712ix
    @m4712ix Před 8 lety

    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.

  • @arafe-zawad-sajid
    @arafe-zawad-sajid Před 8 lety

    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?

  • @Vysair
    @Vysair Před 2 lety

    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

  • @bunnycat102
    @bunnycat102 Před 10 lety +3

    THANKS SO MUCH! Man you have no idea how much you're saving my ass!

  • @MC-Minority
    @MC-Minority Před 4 lety

    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.

  • @user-vp4sc7rp7w
    @user-vp4sc7rp7w Před 10 lety +1

    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.

  • @math260879
    @math260879 Před 4 lety

    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 ?

  • @1brixian
    @1brixian Před 15 lety +1

    Java never looked so easy.Great tutorials.

  • @iczybear
    @iczybear Před rokem

    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?

  • @EpochIsEpic
    @EpochIsEpic Před 4 lety +9

    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.

  • @kryzystof
    @kryzystof Před 5 lety

    Preparing for my java exam, so thanks for a clear and precise explanation.
    God speed

  • @angryfish8394
    @angryfish8394 Před 3 lety

    That was the simplest way to use a loop on a try catch thanks for the video my dud 👌

  • @jeffr5332
    @jeffr5332 Před 8 lety

    Awesome explanation, you made a confusing java subject much clearer.

  • @apachejim8308
    @apachejim8308 Před 8 lety +4

    it's been six years, how time flies

    • @naseemgharibi
      @naseemgharibi Před 6 lety

      it's going to be a 9 years lol i hope you get a notification on my comment xD

  • @autumnvalls2121
    @autumnvalls2121 Před 7 lety

    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.

  • @iphoneusdsd
    @iphoneusdsd Před 8 lety

    Thanks helped for a reminder before an exam.

  • @maurok4560
    @maurok4560 Před 6 lety +1

    Always come to thenewboston after class

  • @Lena-of7wd
    @Lena-of7wd Před 5 lety

    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!

  • @davidvondran3465
    @davidvondran3465 Před 7 lety

    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?

  • @JinGwee
    @JinGwee Před 9 lety

    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?

    • @zweiche
      @zweiche Před 9 lety +1

      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;
      }

  • @harivigneshr6244
    @harivigneshr6244 Před 2 lety +1

    Thank you for posting this video. What would be the Java code to write text into an existing word (.docx) file?

  • @LL-yi4pb
    @LL-yi4pb Před 7 lety

    you are the best at explaining Java that I've found on CZcams :)

  • @awaqar2
    @awaqar2 Před 7 lety

    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

  • @wyattwhitney2891
    @wyattwhitney2891 Před 5 lety

    coming in clutch for finals. thank you so much man. i'm glad I found your channel.

  • @nick_jacob
    @nick_jacob Před 9 lety

    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 :)

    • @Brax1982
      @Brax1982 Před 9 lety

      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.

    • @nick_jacob
      @nick_jacob Před 9 lety

      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.

  • @DipeshRafalia
    @DipeshRafalia Před 7 lety

    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 .

  • @renbll
    @renbll Před 6 lety

    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? ^^

  • @mohaarulez
    @mohaarulez Před 10 lety

    Correct me if i'm wrong, but in the console you get an ArhitmeticException, So isnt there any special exception for that?

  • @cy4n_knight_q8
    @cy4n_knight_q8 Před 4 lety +1

    9 years and still better than college lecture

  • @nikeshbro8612
    @nikeshbro8612 Před 4 lety

    this is the coolest tutorial that i ever watched in youtube

  • @MLGvonyar
    @MLGvonyar Před 6 lety

    I know this video is old but you are a life saver bro!!!!

  • @jafarkay
    @jafarkay Před 14 lety

    almost done with the whole series!!!!!!!!! YOU ROCK SIR... YOU ROCK!!

  • @mandyzhang4492
    @mandyzhang4492 Před 6 lety

    Midterm today , excellent video 9min video better than 50 min lecture :)

  • @Mearthzy
    @Mearthzy Před 9 lety

    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

  • @monicaceja6889
    @monicaceja6889 Před 6 lety

    I literally learned more from you then from school

  • @0510Gaston
    @0510Gaston Před 11 lety

    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

  • @ValiusTrigger
    @ValiusTrigger Před 9 lety

    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

  • @adrianvillanueva3639
    @adrianvillanueva3639 Před 4 lety

    BRO YOU HELP ME TO UNDERSTAND WELL THIS TRY AND CATCH METHOD, RATHER THAN IN MY SCHOOL THNXX ALOT BRODIE !

  • @leonalexandrov5440
    @leonalexandrov5440 Před 3 lety

    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

  • @TawabulIslam
    @TawabulIslam Před 6 lety

    would not a simple while loop work as same? cause while x=1 its gonna run the code inside the while loop once.

  • @Sasa-qb6zw
    @Sasa-qb6zw Před 8 lety

    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?

    • @BradleyGibbs
      @BradleyGibbs Před 8 lety +1

      Fix? It's not broken. You cannot put doubles in ints. If you want to use doubles just change the variables to double.

  • @PaulKruskamp
    @PaulKruskamp Před 9 lety +11

    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.

    • @Junior-rz2gx
      @Junior-rz2gx Před 5 lety

      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?

    • @Kal-EL_Volta
      @Kal-EL_Volta Před 5 lety

      @@Junior-rz2gx I have the same question, did you figure it out?

    • @Junior-rz2gx
      @Junior-rz2gx Před 5 lety

      @@Kal-EL_Volta I have no idea, we left to study another chapter, I just know that I gotta have it there.

    • @SinghDarren
      @SinghDarren Před 5 lety +1

      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

  • @gallegom58
    @gallegom58 Před 10 lety +3

    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);
    }

    • @jeremysoronen8154
      @jeremysoronen8154 Před 10 lety +5

      Add this to your catch area after the error message:
      input.nextLine();

    • @JPxKillz
      @JPxKillz Před 10 lety

      Jeremy Soronen Why does this work, and why does it happen? Happened to me to.

    • @MashrufKabir
      @MashrufKabir Před 9 lety

      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.

    • @MashrufKabir
      @MashrufKabir Před 9 lety +1

      Wait, it's supposed to be...an infinite loop. That's what he did up there.

    • @zoheyr106
      @zoheyr106 Před 9 lety

      you write x1+x2
      make it x1/x2

  • @cristiandragan3698
    @cristiandragan3698 Před 7 lety +1

    Explained it very well, thank you!

  • @KnealForTheMeal
    @KnealForTheMeal Před 8 lety +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.

  • @enzojorge8914
    @enzojorge8914 Před 7 lety

    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;}
    }

  • @SAli-bl3nb
    @SAli-bl3nb Před 7 lety +1

    When I try to implement this, the loop just continues printing "Enter the x value". Help pls

  • @adipratapsinghaps
    @adipratapsinghaps Před 11 lety

    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.

  • @KomutanLogarMi
    @KomutanLogarMi Před 7 lety

    Thanks for pure speaking about that lesson i understood all thank you so much you are the best teacher :) love u

  • @MP501st
    @MP501st Před 10 lety +28

    R.I.P apple.java ;(

  • @namesare4fools
    @namesare4fools Před 8 lety

    How do I differentiate the errors such as
    2/0 = error 1
    dsverf = error 2 (Because i input a string instead of int)

    • @Malek97
      @Malek97 Před 7 lety

      build more catch's from specified to general error

  • @BigEaston2
    @BigEaston2 Před 10 lety

    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.

  • @olivialemaire2512
    @olivialemaire2512 Před 10 lety +14

    Bucky fixed a spelling error! :O

  • @user-rh7hh3up5k
    @user-rh7hh3up5k Před rokem

    Man still now its one of the best .

  • @biancadenicechua8504
    @biancadenicechua8504 Před 7 lety

    You saved my life in Object programming class 😭

  • @hotboy6164
    @hotboy6164 Před 8 lety

    just making it so simple for us to understand thank you!!

  • @unknownghost8128
    @unknownghost8128 Před 5 lety

    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??

  • @nunobone
    @nunobone Před 10 lety +13

    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

  • @joshuagriffiths3991
    @joshuagriffiths3991 Před 8 lety

    Thanks thenewboston. A great explanation, just what I needed.

  • @goranmarinkovic9143
    @goranmarinkovic9143 Před 7 lety

    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!

  • @HalXV
    @HalXV Před 8 lety +58

    2:45 hahahahahahahaha this guy is my favorite teacher.

    • @developmentstuff7738
      @developmentstuff7738 Před 8 lety +6

      lol, I laughed at that part, immediately checked comments if someone enjoyed this too. Also he continues his joke at 7:00

    • @princegupta6779
      @princegupta6779 Před 8 lety

      You're right he is the best teacher.

    • @fizzlid4511
      @fizzlid4511 Před 7 lety

      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.

  • @LiudaMikha
    @LiudaMikha Před 14 lety

    Thanx! I have exam today, watching you tutorials for revision. So clear and understandable! =)
    you are awesome :-)

  • @1983saulomoreira
    @1983saulomoreira Před 3 lety

    Very much useful and simple explanation, thank you very much

  • @khumbelomulelu9419
    @khumbelomulelu9419 Před 5 lety

    BETTER THAN MOST UNIVERSITY LECTURERS

  • @canaldoviolaoinstrumental1964

    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?

  • @paoloberg70
    @paoloberg70 Před 6 lety

    great tutorial, opening a lot of good topics and give clues

  • @saxena3718
    @saxena3718 Před 8 lety

    i watch your video one day before the exam and pass every single fucking time.........so thank you

  • @mukkaprateek1341
    @mukkaprateek1341 Před 4 lety

    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

  • @katruya
    @katruya Před 6 lety

    Um, I did the exercise here and instead of catching the exception the console output said: Infinity ????? wat.jpg