Skip to content

Commit 49fc0d8

Browse files
update exception (#200)
* update exception * fixed Serial error on gradle
1 parent 15ac95c commit 49fc0d8

16 files changed

+225
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.exception;
2+
3+
public class ArithmeticExceptionExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.exception;
2+
3+
public class ClassCastExceptionExample {
4+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.basics.exception;
2+
3+
public class CustomExceptionExample extends Exception {
4+
5+
// @java.io.Serial
6+
private static final long serialVersionUID = 234122876006267687L;
7+
8+
public CustomExceptionExample() {
9+
super();
10+
}
11+
12+
public CustomExceptionExample(String message) {
13+
super(message);
14+
}
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.exception;
2+
3+
public class FinallyExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.exception;
2+
3+
public class IndexOutOfBoundsExceptionExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.exception;
2+
3+
public class NullPointerExceptionExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.exception;
2+
3+
public class NumberFormatExceptionExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.exception;
2+
3+
public class ThrowsExceptionExample {
4+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.examplehub.basics.exception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ArithmeticExceptionExampleTest {
8+
@Test
9+
void testDivideByZero() {
10+
try {
11+
int division = 10 / 0;
12+
fail();
13+
} catch (ArithmeticException e) {
14+
assertTrue(true);
15+
}
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.examplehub.basics.exception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
9+
class ClassCastExceptionExampleTest {
10+
@Test
11+
void testClassCast() {
12+
try {
13+
String[] strArray = new String[]{"John", "Snow"};
14+
ArrayList<String> strList = (ArrayList<String>) Arrays.asList(strArray);
15+
System.out.println("String list: " + strList);
16+
} catch (ClassCastException e) {
17+
assertTrue(true);
18+
}
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.examplehub.basics.exception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class CustomExceptionExampleTest {
8+
@Test
9+
void testCustomException() {
10+
try {
11+
throw new CustomExceptionExample("custom exception");
12+
} catch (CustomExceptionExample e) {
13+
assertEquals("custom exception", e.getMessage());
14+
}
15+
}
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.examplehub.basics.exception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class FinallyExampleTest {
8+
@Test
9+
void testWithCatch() {
10+
int result = 0;
11+
try {
12+
int number = 10 / 2;
13+
} finally {
14+
result++;
15+
}
16+
assertEquals(1, result);
17+
}
18+
19+
@Test
20+
void testReturn() {
21+
assertEquals(3, func());
22+
}
23+
public int func() {
24+
try {
25+
int num = 10 / 0;
26+
return 1;
27+
} catch (ArithmeticException e) {
28+
return 2;
29+
}finally {
30+
return 3;
31+
}
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.examplehub.basics.exception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class IndexOutOfBoundsExceptionExampleTest {
8+
@Test
9+
void testArrayAccess() {
10+
int[] nums = {1, 2, 3, 4, 5};
11+
try {
12+
int sum = 0;
13+
for (int i = 0; i <= nums.length; ++i) {
14+
sum += nums[i];
15+
}
16+
System.out.println("sum = " + sum);
17+
fail();
18+
} catch (IndexOutOfBoundsException e) {
19+
assertTrue(true);
20+
}
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.examplehub.basics.exception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class NullPointerExceptionExampleTest {
8+
@Test
9+
void testNullPointer() {
10+
try {
11+
String str = null;
12+
System.out.println(" str length = " + str.length());
13+
fail();
14+
} catch (NullPointerException e) {
15+
assertTrue(true);
16+
}
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.examplehub.basics.exception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class NumberFormatExceptionExampleTest {
8+
@Test
9+
void testFormatNumber() {
10+
try {
11+
int number = Integer.parseInt("123a");
12+
fail();
13+
} catch (NumberFormatException e) {
14+
assertTrue(true);
15+
}
16+
}
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.examplehub.basics.exception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ThrowsExceptionExampleTest {
8+
@Test
9+
void testReceiveException() {
10+
try {
11+
int num = division(3, 0);
12+
fail();
13+
} catch (ArithmeticException e) {
14+
assertTrue(true);
15+
}
16+
}
17+
18+
public int division(int a, int b) throws ArithmeticException {
19+
return a / b;
20+
}
21+
22+
@Test
23+
void testThrowException() {
24+
try {
25+
int number = parseInt("123a");
26+
fail();
27+
} catch (NumberFormatException e) {
28+
assertTrue(true);
29+
assertEquals("can't contains non-digits", e.getMessage());
30+
}
31+
}
32+
33+
public int parseInt(String str) throws NumberFormatException{
34+
if (!str.matches("\\d+")){
35+
throw new NumberFormatException("can't contains non-digits");
36+
}
37+
return Integer.parseInt(str);
38+
}
39+
}

0 commit comments

Comments
 (0)