Mastering Java and Spring Boot - Live Course : https://bit.ly/TeluskoJavaLive
For More Queries WhatsApp or Call on : +919008963671


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 : https://github.com/navinreddy20/Javacode.git

Java:- https://bit.ly/JavaUdemyTelusko
Spring:- https://bit.ly/SpringUdemyTelusko

More Learning :

Java :- https://bit.ly/3x6rr0N
Python :- https://bit.ly/3GRc7JX
Django :- https://bit.ly/3MmoJK6

JavaScript :- https://bit.ly/3tiAlHo
Node JS :- https://bit.ly/3GT4liq

Rest Api :-https://bit.ly/3MjhZwt
Servlet :- https://bit.ly/3Q7eA7k
Spring Framework :- https://bit.ly/3xi7buh
Design Patterns in Java :- https://bit.ly/3MocXiq
Docker :- https://bit.ly/3xjWzLA

Blockchain Tutorial :- https://bit.ly/3NSbOkc
Corda Tutorial:- https://bit.ly/3thbUKa
Hyperledger Fabric :- https://bit.ly/38RZCRB

NoSQL Tutorial :- https://bit.ly/3aJpRuc
Mysql Tutorial :- https://bit.ly/3thpr4L

Data Structures using Java :- https://bit.ly/3MuJa7S
Git Tutorial :- https://bit.ly/3NXyCPu

Donation:
PayPal Id : navinreddy20
https://www.telusko.com