1+ // -- Multiple Exceptions -- //
2+
3+ public class Multiple_Exceptions {
4+ public static void main (String [] args ) {
5+
6+ System .out .println ("Handling Multiple Exceptions in Java\n " );
7+
8+ // ArithmeticException
9+ try {
10+ int a = 10 ;
11+ int b = 0 ;
12+ int result = a / b ;
13+ System .out .println ("Result: " + result );
14+ } catch (ArithmeticException e ) {
15+ System .out .println ("ArithmeticException: Cannot divide by zero" );
16+ }
17+
18+ // NullPointerException
19+ try {
20+ String str = null ;
21+ System .out .println (str .length ());
22+ } catch (NullPointerException e ) {
23+ System .out .println ("NullPointerException: String is null" );
24+ }
25+
26+ // NumberFormatException
27+ try {
28+ String num = "abc" ;
29+ int number = Integer .parseInt (num );
30+ System .out .println ("Number: " + number );
31+ } catch (NumberFormatException e ) {
32+ System .out .println ("NumberFormatException: Invalid number format" );
33+ }
34+
35+ // ArrayIndexOutOfBoundsException
36+ try {
37+ int arr [] = {1 , 2 , 3 };
38+ System .out .println (arr [5 ]);
39+ } catch (ArrayIndexOutOfBoundsException e ) {
40+ System .out .println ("ArrayIndexOutOfBoundsException: Invalid array index" );
41+ }
42+
43+ // StringIndexOutOfBoundsException
44+ try {
45+ String s = "Java" ;
46+ System .out .println (s .charAt (10 ));
47+ } catch (StringIndexOutOfBoundsException e ) {
48+ System .out .println ("StringIndexOutOfBoundsException: Invalid string index" );
49+ }
50+
51+ System .out .println ("Program continues after handling all exceptions." );
52+ }
53+ }
0 commit comments