Skip to content

PRO TIP Block youtube.com at the DNS level — Pi-hole, NextDNS or your hosts file — but allow youtube-nocookie.com and i.ytimg.com. These tutorials keep playing; the rabbit hole does not.

Learning without distractions

#78 Try with Multiple Catch in Java

181.7K views on YouTube

Check out our courses:

Mastering Agentic AI with Java : https://go.telusko.com/agentic-ai
Coupon: TELUSKO10 (10% Discount)

DevOps Bootcamp: https://go.telusko.com/complete-devops
Coupon: TELUSKO10 (10% Discount)

Master Java Spring Development : https://go.telusko.com/learn-master
Coupon: TELUSKO10 (10% Discount)

For More Queries WhatsApp or Call on : +919008963671

website : https://telusko.com/

Udemy Courses:

Java Spring:- https://go.telusko.com/Udemyjavaspring
Java:- https://go.telusko.com/udemyteluskojava
Spring: https://go.telusko.com/udemyteluskospring
Java For Programmers:- https://go.telusko.com/javaProgrammers
Python : https://go.telusko.com/udemyteluskopython
Git : https://go.telusko.com/udemyteluskogit
Docker : https://go.telusko.com/udemyteluskodocker

Instagram : https://www.instagram.com/navinreddyofficial/
Linkedin : https://in.linkedin.com/in/navinreddy20
WhatsApp : https://go.telusko.com/whatsapp
TELUSKO Android App : https://go.telusko.com/TELUSKOAPP
TELUSKO IOS App : https://apple.co/3SsgmU2
Discord : https://discord.gg/D8hWe9BqfF

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

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