-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExceptionHandling.java
49 lines (44 loc) · 1.36 KB
/
ExceptionHandling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package Examples;
import java.util.Scanner;
// Exception Handling Example 1
/*
public class ExceptionHandling {
public static void main(String[] args) {
int num1, num2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter 1st Number (Dividend): ");
num1 = scanner.nextInt();
System.out.print("Enter 2nd Number (Divisor): ");
num2 = scanner.nextInt();
scanner.close();
try {
int result = num1 / num2;
System.out.print("Quotient: " + result);
} catch (Exception e) {
// TODO: handle exception
System.out.println("Divisor should not be Zero, else it\s \"INFINITY\".");
System.out.println("ERROR: " + e);
}
}
}
*/
// Example 2
class Example{
public static void main(String args[]){
try{
int arr[]=new int[7];
arr[10]=10/5;
System.out.println("Last Statement of try block");
}
catch(ArithmeticException e){
System.out.println("You should not divide a number by zero");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Accessing array elements outside of the limit");
}
catch(Exception e){
System.out.println("Some Other Exception");
}
System.out.println("Out of the try-catch block");
}
}