Skip to content

Commit 2521325

Browse files
committed
Exceptions
1 parent 6c9001a commit 2521325

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class ArithmeticExceptionClass {
2+
3+
void m1() {
4+
5+
System.out.println("Start m1");
6+
m2();
7+
System.out.println("End m1");
8+
}
9+
void m2() {
10+
11+
System.out.println(10/0);
12+
}
13+
public static void main(String[] args) {
14+
15+
System.out.println("Start Main");
16+
ArithmeticExceptionClass obj = new ArithmeticExceptionClass();
17+
obj.m1();
18+
System.out.println("End Main");
19+
}
20+
}
21+
22+
/*
23+
O/P :
24+
Start Main
25+
Start m1
26+
Exception in thread "main" java.lang.ArithmeticException: / by zero
27+
at ArithmeticExceptionClass.m2(1_ArithmeticException.java:11)
28+
at ArithmeticExceptionClass.m1(1_ArithmeticException.java:6)
29+
at ArithmeticExceptionClass.main(1_ArithmeticException.java:17)
30+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class NullPointerExceptionClass {
2+
3+
void m1() {
4+
5+
System.out.println("IN m1");
6+
}
7+
void m2() {
8+
9+
System.out.println("IN m2");
10+
}
11+
public static void main(String[] args) {
12+
13+
NullPointerExceptionClass obj = new NullPointerExceptionClass();
14+
obj.m1();
15+
16+
obj = null;
17+
18+
obj.m2();
19+
}
20+
}
21+
22+
/*
23+
*O/P : IN m1
24+
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "NullPointerExceptionClass.m2()" because "<local1>" is null
25+
at NullPointerExceptionClass.main(1_NullPointerException.java:18)
26+
27+
*/

Exception-Handling/3_IOException.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.io.*;
2+
3+
class IOExceptionClass {
4+
5+
public static void main(String[] args) {
6+
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
9+
int data = Integer.parseInt(br.readLine());
10+
System.out.println(data);
11+
}
12+
}
13+
14+
/*
15+
* O/P : Error: unreported exception IOException; must be caught or declared to be thrown
16+
int data = Integer.parseInt(br.readLine());
17+
^
18+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.io.*;
2+
3+
class NumberFormatExceptionClass {
4+
5+
public static void main(String[] args) throws IOException {
6+
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
9+
System.out.println("Enter Integer Value : ");
10+
int data = Integer.parseInt(br.readLine());
11+
System.out.println(data);
12+
}
13+
}
14+
15+
/*
16+
* O/P : Enter Integer Value :
17+
dhiraj
18+
Exception in thread "main" java.lang.NumberFormatException: For input string: "dhiraj"
19+
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
20+
at java.base/java.lang.Integer.parseInt(Integer.java:665)
21+
at java.base/java.lang.Integer.parseInt(Integer.java:781)
22+
at NumberFormatExceptionClass.main(4_NumberFormatException.java:10)
23+
*/

0 commit comments

Comments
 (0)