#78 Try with Multiple Catch in Java

Поделиться
HTML-код
  • Опубликовано: 17 янв 2023
  • 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 about try with multiple catch:
    --In Java, the try-catch block is used to handle exceptions. It allows you to write code that handles exceptions that may be thrown during the execution of your program.
    we can also handle different types of exceptions with multiple catch blocks.
    -- suppose we write few lines of code and we donot know which line can generate exception but
    we know donot which types of exception can be generated. In this case we can use try with multiple catch
    e.g
    int num=4;
    int arr[]={3,4,5};
    try{
    int result=40/num; //we donot know what does user pass the value of num then we put inside try
    System.out.println(arr[result]); //we donot know what the value of result but we know index of array allowed is 0 to 2 therefore we put this statement also in try
    }
    catch(ArithmeticException ae){
    System.out.println(ae);
    }
    catch(ArrayIndexOutOfBoundsException aio){
    System.out.println(aio);
    }
    #
    Handling parents and child Exception both
    -- when catching both child and parent exceptions in a try-catch block, it is generally recommended to catch the child
    exceptions before the parent exception.
    -- The reason for this is that if you catch the parent exception before the child exception, the catch block for the parent exception will also catch any child exceptions that are subclasses of the parent exception.
    This can make it more difficult to handle the child exceptions separately.
    e.g
    int a = 10;
    int arr[]={3,4,5};
    try{
    int b = 3/a;
    System.out.println(arr[b]);
    }
    catch(Exception e){
    System.out.print("parent class of every exception");
    }
    catch(ArithmeticException e){
    }
    -- this will give compile time error i.e error: exception ArithmeticException has already been caught by Exception class
    Right way:
    first child Exception then parents Exception
    int a = 10;
    int arr[]={3,4,5};
    try{
    int b = 3/a;
    System.out.println(arr[b]);
    }
    catch(ArithmeticException e){
    }
    catch(Exception e){
    System.out.print("parent class of every exception");
    }
    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
  • НаукаНаука

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

  • @ronatkumarmuni4076
    @ronatkumarmuni4076 7 месяцев назад +3

    This play list is most use full who is learning java in 2023.

  • @GaganDeep-xz8kf
    @GaganDeep-xz8kf 11 месяцев назад

    Thankyou Sir, Learning a lot in begining of job from your videos.

  • @ayodhyarode4522
    @ayodhyarode4522 9 месяцев назад

    I understood concept of exception. Thank you so much navin sir for this fantastic tutorial ❤🙏

  • @04.anugrahabar25
    @04.anugrahabar25 5 месяцев назад

    Beautifully explained ❤

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

    Thank you sir❤❤

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

    Helpful 🙂

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

    Fantastic

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

    Amazing 🎉

  • @user-ez6pz1bl2v
    @user-ez6pz1bl2v 3 месяца назад

    Amazing

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

    Can we say the exceptions are childs of of Exception Class and the print methode override

  • @anuragpathak7939
    @anuragpathak7939 10 месяцев назад

    I tried to implement the same code but inside catch there is an error showing "No exception of type Exception can be thrown; an exception type must be a subclass of Throwable". Why it's not considering Exception?

  • @actandrepeat
    @actandrepeat Год назад +3

    Lit