-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionDemo.java
More file actions
47 lines (41 loc) · 1.54 KB
/
ExceptionDemo.java
File metadata and controls
47 lines (41 loc) · 1.54 KB
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
import java.util.Scanner;
public class ExceptionDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//Arithmetic exception
try {
System.out.println("Divide 10 by");
int num = scan.nextInt();
System.out.println("The result: " + 10/num);
} catch (ArithmeticException e) {
System.out.println("Number cannot be divided by 0!");
}
//Number Format exception
try{
System.out.println("Enter a number");
int parsedNumber = Integer.parseInt(scan.next());
System.out.println("Parsed Int = " + parsedNumber);
} catch(NumberFormatException e){
System.out.println("Invalid Number format!");
}
//array index out of bound exception
try {
int[] array = {1,2,3};
System.out.println("Accessable index 0-2: ");
int access = scan.nextInt();
System.out.println("Array value: " + array[access]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid Index!");
}
//NegativeArray size exception
try {
System.out.println("enter the array size");
int size = scan.nextInt();
int[] arr = new int[size];
System.out.println("Array created int the size: " + size);
} catch (NegativeArraySizeException e) {
System.out.println("Array size cannot be negative!");
}
scan.close();
}
}