Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,985 questions

51,929 answers

573 users

How to catch multiple exceptions in the same catch clause with Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        try {
            int[] arr = {1, 2, 3, 4, 5, 6, 7};
               
            arr[19] = 938;
           
        } catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
            System.out.println("Exception: " + e);
        }
    }
}
 
 
 
 
/*
run:

Exception: java.lang.ArrayIndexOutOfBoundsException: Index 19 out of bounds for length 7

*/

 



answered Oct 25, 2023 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        try {
            int[] arr = {1, 2, 3, 4, 5, 6, 7};
               
            arr[19] = 938;
           
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Exception 1: " + e);
        } catch (ArithmeticException e) {
            System.out.println("Exception 2: " + e);
        }
    }
}
 
 
 
 
/*
run:

Exception 1: java.lang.ArrayIndexOutOfBoundsException: Index 19 out of bounds for length 7

*/

 



answered Oct 25, 2023 by avibootz

Related questions

1 answer 192 views
1 answer 168 views
168 views asked Dec 17, 2016 by avibootz
1 answer 146 views
4 answers 292 views
1 answer 109 views
109 views asked Jan 24, 2024 by avibootz
1 answer 230 views
1 answer 80 views
...