#80 Exception throw keyword in Java

Поделиться
HTML-код
  • Опубликовано: 10 янв 2025
  • Check out our courses:
    Java Full Stack and Spring AI - go.telusko.com...
    Coupon: TELUSKO10 (10% Discount)
    DevOps with AWS: From Basics to Mastery : go.telusko.com...
    Coupon: TELUSKO10 (10% Discount)
    Master Java Spring Development : go.telusko.com...
    Coupon: TELUSKO20 (20% Discount)
    For More Queries WhatsApp or Call on : +919008963671
    Udemy Courses:
    Spring: go.telusko.com...
    Java:- go.telusko.com...
    Java Spring:- go.telusko.com...
    Java For Programmers:- go.telusko.com...
    Python : go.telusko.com...
    Git : go.telusko.com...
    Docker : go.telusko.com...
    website : courses.telusk...
    In this lecture we are discussing throw keyword :
    #1
    -- throw keyword in Java is used to explicitly throw an exception.
    When an exception is thrown using the throw keyword, the execution of the current method is stopped.
    syntax-
    throw new NullPointerException("Object is null");
    e.g
    public void divide(int a, int b) {
    if (b == 0) {
    throw new ArithmeticException("Cannot divide by zero");
    }
    int result = a / b;
    System.out.println(result);
    }
    #2
    -- throw keyword is used to throw exceptions, not to catch them. To catch exceptions, you need to use a try-catch block.
    -- if you do not use try-catch then the control is passed, where the method is called.
    -- when you want to pass message to an exception
    then you should use the parametrized constructor instead non parameterized constructor
    e.g
    class Main{
    public static void main(String []args){
    int a=0;
    try{
    if(a==0)
    throw new ArithmeticException("a should not be zero"); // throw new ArithmeticException() is non parametrized constructor
    }
    catch(ArithmeticException e){
    System.out.println("Exception caught: "+e);
    }
    }
    }
    Output: Exception caught: java.lang.ArithmeticException: a should not be zero
    class Main{
    public static void main(String[] args){
    int j=30;
    int i=1;
    try{
    j=18/i;
    //handle the exception using throw
    if(j==0)
    throw new ArithmeticException("i donot want to print 0"); //try to create an exception
    }
    catch(ArithmeticException e){//here catch is catching the exception
    System.out.println("ArithmeticException caught");
    System.out.println(e.getMessage()); //getMessage() method use to get the message
    }
    catch(Exception e){
    System.out.println("Exception caught");
    }
    }
    }
    Github repo : github.com/nav...
    Java:- bit.ly/JavaUde...
    Spring:- bit.ly/SpringU...
    More Learning :
    Java :- bit.ly/3x6rr0N
    Python :- bit.ly/3GRc7JX
    Django :- bit.ly/3MmoJK6
    JavaScript :- bit.ly/3tiAlHo
    Node JS :- bit.ly/3GT4liq
    Rest Api :-bit.ly/3MjhZwt
    Servlet :- bit.ly/3Q7eA7k
    Spring Framework :- bit.ly/3xi7buh
    Design Patterns in Java :- bit.ly/3MocXiq
    Docker :- bit.ly/3xjWzLA
    Blockchain Tutorial :- bit.ly/3NSbOkc
    Corda Tutorial:- bit.ly/3thbUKa
    Hyperledger Fabric :- bit.ly/38RZCRB
    NoSQL Tutorial :- bit.ly/3aJpRuc
    Mysql Tutorial :- bit.ly/3thpr4L
    Data Structures using Java :- bit.ly/3MuJa7S
    Git Tutorial :- bit.ly/3NXyCPu
    Donation:
    PayPal Id : navinreddy20
    www.telusko.com

Комментарии • 26

  • @SP-vq7tg
    @SP-vq7tg 8 месяцев назад +13

    Something different from your tutorials compared to others is, it is very engaging. Never feels bored even for a second. Thankyou so much Navin!!

  • @lukegleeson3970
    @lukegleeson3970 4 месяца назад +3

    feel like im on ther home run of this tutorial. not quite done yet but thank you so much for this content. its crazy that in this world you can learn to code without even player a doller.... thanks telusko. you're a legend.

  • @vishwajithbarad
    @vishwajithbarad 2 месяца назад

    at 4:02 the exception is caused by the 9th line and the throw exception command will not execute since it will directly jump to catch with the same ArithmeticException. instead you could have move lines 10 and 11 to 9 and 9 to 11. but you have cleared the concept.

  • @sowmyaks8386
    @sowmyaks8386 Год назад +11

    Thankyou for the explanation sir. Following your java lectures.... enjoying it♥️🙂🙂♥️

  • @ajayghode3602
    @ajayghode3602 13 дней назад

    Thank you...after longtime I got to know the real use of throw keyword.
    @4:54 if i's value 0 then output : it shows /zero.
    and if i's value 20 then output: I don't want to print zero.

  • @udara-me
    @udara-me 5 месяцев назад

    The best java course ever😍

  • @workoutsidhu
    @workoutsidhu 11 месяцев назад

    Sir Your video is very help full.
    Thank You

  • @praneshnanoskar6353
    @praneshnanoskar6353 Год назад +2

    Superb explanation 👍

  • @allabout90minutes9
    @allabout90minutes9 Год назад

    Amazing explanation

  • @sukanyaswaminathan9130
    @sukanyaswaminathan9130 8 месяцев назад +1

    hello sir,
    on executing line 9 itself, error occurs and the execution enters the catch block rite ? Line number 10 won't be executed at all and hence the 'throw' code. Am i getting it rite ?

  • @Jonggungoo
    @Jonggungoo Год назад +5

    aren't u from telangana sir!

    • @bengaming99
      @bengaming99 Год назад

      That’s why the channel name is telusko which means (learn) in Telugu

  • @sethupathys641
    @sethupathys641 Год назад +2

    Hey, what does the throws keyword do?

    • @crofux
      @crofux 6 месяцев назад

      Throws keyword indicates that there might be error in this block of code

  • @maleeshasandakalum6213
    @maleeshasandakalum6213 Год назад

    Thank you sir❤❤

  • @MudrikaAgrawal-kx6zk
    @MudrikaAgrawal-kx6zk 6 месяцев назад

    I have one doubt, how ArithmeticException object becomes constructor?

    • @shindeprem228
      @shindeprem228 5 месяцев назад

      Every time we create object using new className()
      the constructor of that class is invoked depending on parameters we pass, he meant that

  • @tahaalshimy8255
    @tahaalshimy8255 Год назад

    God bless you

  • @jawad_karim
    @jawad_karim Год назад

    amazing !

  • @oshadhaedirisinghe1455
    @oshadhaedirisinghe1455 11 месяцев назад

    Thank you

  • @syedadil7256
    @syedadil7256 Год назад

    There is an error in the if part it is if(i==0) but u placed if(j==0). As the denominator should be checked that is i

    • @ananysharma9290
      @ananysharma9290 Год назад +6

      Nope buddy, If you'll re-watch the video, he explained that he doesn't want the value of J to be zero( which comes as an output when 18 is divided by 20).
      Since the above division operation is not an exception, catch block is not called, that's why we are using the keyword "Throw" to make this operation an exception and catched by catch block.
      The code in the video is totally correct 🙂

    • @syedadil7256
      @syedadil7256 Год назад

      @@ananysharma9290 ok dude tqs

  • @marioponticiello297
    @marioponticiello297 Год назад

    don pollo