-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaExceptions.java
More file actions
86 lines (78 loc) · 2.41 KB
/
JavaExceptions.java
File metadata and controls
86 lines (78 loc) · 2.41 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.PrintWriter;
public class JavaExceptions {
void ArithmeticExceptionDemo() {
int number = 10;
try {
// int output = number / 10; // it's gives you 1
int output = number / 0;
System.out.println("Number is : " + output);
} catch (ArithmeticException ec) {
// System.out.println(ec.getMessage().toString());
System.out.println(ec);
}
}
void NullPointerExceptionDemo() {
try {
String st = null;
System.out.println(st.length());
} catch (NullPointerException ec) {
System.out.println(ec);
}
}
void numberFormatExceptiondemo() {
try {
String st = "Hello";
int test = Integer.parseInt(st);
System.out.println(test);
} catch (Exception ec) {
System.out.println(ec);
}
}
void ArayindexOutoffnboundDemo() {
try {
char[] ac = new char[20];
ac[26] = 59;
System.out.println("Good");
} catch (Exception ec) {
System.out.println(ec);
}
}
void Exceptiontrycatch() {
int num1 = 100;
int num2 = 0;
try {
System.out.println("result=" + (num1 / num2));
} catch (Exception ec) {
System.out.println("ans= Oops You can't divide by " + num2);
}
}
void ExceptionInexception() {
try {
int ij = 10000 / 0;
System.out.println(ij);
} catch (Exception ec) {
// System.out.println(ec);
int ij2 = 100 / 0;
System.out.println(ij2);
}
}
void fileNotfoundUsingPritWriterException() {
PrintWriter pt;
try {
pt = new PrintWriter("X:\\rest.text");
pt.println("hy");
} catch (Exception ec) {
System.out.println(ec);
}
}
public static void main(String args[]) {
JavaExceptions ExceptionHandling = new JavaExceptions();
ExceptionHandling.ArithmeticExceptionDemo();
ExceptionHandling.NullPointerExceptionDemo();
ExceptionHandling.numberFormatExceptiondemo();
ExceptionHandling.ArayindexOutoffnboundDemo();
ExceptionHandling.Exceptiontrycatch();
// ExceptionHandling.ExceptionInexception();
ExceptionHandling.fileNotfoundUsingPritWriterException();
}
}