#80 Exception throw keyword in Java

Поделиться
HTML-код
  • Опубликовано: 3 авг 2024
  • Check out our courses:
    Spring and Microservices Weekend Live Batch : bit.ly/spring-live-weekend
    Coupon: TELUSKO10 (10% Discount)
    Master Java Spring Development : bit.ly/java-spring-cloud
    Udemy Courses:
    Java:- bit.ly/JavaUdemyTelusko
    Spring:- bit.ly/SpringUdemyTelusko
    Java For Programmers:- bit.ly/javaProgrammers
    For More Queries WhatsApp or Call on : +919008963671
    website : courses.telusko.com/
    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/navinreddy20/Javac...
    Java:- bit.ly/JavaUdemyTelusko
    Spring:- bit.ly/SpringUdemyTelusko
    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
  • НаукаНаука

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

  • @SP-vq7tg
    @SP-vq7tg 2 месяца назад +2

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

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

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

  • @udara-me
    @udara-me 12 дней назад

    The best java course ever😍

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

    Superb explanation 👍

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

    Sir Your video is very help full.
    Thank You

  • @allabout90minutes9
    @allabout90minutes9 8 месяцев назад

    Amazing explanation

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

    Thank you sir❤❤

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

    God bless you

  • @user-zd1ee5mi3l
    @user-zd1ee5mi3l 7 месяцев назад

    amazing !

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

    Thank you

  • @sethupathys641
    @sethupathys641 11 месяцев назад +2

    Hey, what does the throws keyword do?

    • @Codefluxz
      @Codefluxz Месяц назад

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

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

    aren't u from telangana sir!

    • @bengaming99
      @bengaming99 7 месяцев назад

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

  • @sukanyaswaminathan9130
    @sukanyaswaminathan9130 3 месяца назад

    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 ?

  • @MudrikaAgrawal-kx6zk
    @MudrikaAgrawal-kx6zk 25 дней назад

    I have one doubt, how ArithmeticException object becomes constructor?

    • @shindeprem228
      @shindeprem228 16 дней назад

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

  • @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 7 месяцев назад

    don pollo