diff --git a/src/main/java/com/examplehub/basics/exception/ArithmeticExceptionExample.java b/src/main/java/com/examplehub/basics/exception/ArithmeticExceptionExample.java new file mode 100644 index 00000000..eb2aee7e --- /dev/null +++ b/src/main/java/com/examplehub/basics/exception/ArithmeticExceptionExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.exception; + +public class ArithmeticExceptionExample { +} diff --git a/src/main/java/com/examplehub/basics/exception/ClassCastExceptionExample.java b/src/main/java/com/examplehub/basics/exception/ClassCastExceptionExample.java new file mode 100644 index 00000000..e552bd93 --- /dev/null +++ b/src/main/java/com/examplehub/basics/exception/ClassCastExceptionExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.exception; + +public class ClassCastExceptionExample { +} diff --git a/src/main/java/com/examplehub/basics/exception/CustomExceptionExample.java b/src/main/java/com/examplehub/basics/exception/CustomExceptionExample.java new file mode 100644 index 00000000..e998af12 --- /dev/null +++ b/src/main/java/com/examplehub/basics/exception/CustomExceptionExample.java @@ -0,0 +1,15 @@ +package com.examplehub.basics.exception; + +public class CustomExceptionExample extends Exception { + +// @java.io.Serial + private static final long serialVersionUID = 234122876006267687L; + + public CustomExceptionExample() { + super(); + } + + public CustomExceptionExample(String message) { + super(message); + } +} diff --git a/src/main/java/com/examplehub/basics/exception/FinallyExample.java b/src/main/java/com/examplehub/basics/exception/FinallyExample.java new file mode 100644 index 00000000..1ea4f749 --- /dev/null +++ b/src/main/java/com/examplehub/basics/exception/FinallyExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.exception; + +public class FinallyExample { +} diff --git a/src/main/java/com/examplehub/basics/exception/IndexOutOfBoundsExceptionExample.java b/src/main/java/com/examplehub/basics/exception/IndexOutOfBoundsExceptionExample.java new file mode 100644 index 00000000..fdd95fac --- /dev/null +++ b/src/main/java/com/examplehub/basics/exception/IndexOutOfBoundsExceptionExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.exception; + +public class IndexOutOfBoundsExceptionExample { +} diff --git a/src/main/java/com/examplehub/basics/exception/NullPointerExceptionExample.java b/src/main/java/com/examplehub/basics/exception/NullPointerExceptionExample.java new file mode 100644 index 00000000..07709fd7 --- /dev/null +++ b/src/main/java/com/examplehub/basics/exception/NullPointerExceptionExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.exception; + +public class NullPointerExceptionExample { +} diff --git a/src/main/java/com/examplehub/basics/exception/NumberFormatExceptionExample.java b/src/main/java/com/examplehub/basics/exception/NumberFormatExceptionExample.java new file mode 100644 index 00000000..3fa72c6b --- /dev/null +++ b/src/main/java/com/examplehub/basics/exception/NumberFormatExceptionExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.exception; + +public class NumberFormatExceptionExample { +} diff --git a/src/main/java/com/examplehub/basics/exception/ThrowsExceptionExample.java b/src/main/java/com/examplehub/basics/exception/ThrowsExceptionExample.java new file mode 100644 index 00000000..2c42aab7 --- /dev/null +++ b/src/main/java/com/examplehub/basics/exception/ThrowsExceptionExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.exception; + +public class ThrowsExceptionExample { +} diff --git a/src/test/java/com/examplehub/basics/exception/ArithmeticExceptionExampleTest.java b/src/test/java/com/examplehub/basics/exception/ArithmeticExceptionExampleTest.java new file mode 100644 index 00000000..04850753 --- /dev/null +++ b/src/test/java/com/examplehub/basics/exception/ArithmeticExceptionExampleTest.java @@ -0,0 +1,17 @@ +package com.examplehub.basics.exception; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ArithmeticExceptionExampleTest { + @Test + void testDivideByZero() { + try { + int division = 10 / 0; + fail(); + } catch (ArithmeticException e) { + assertTrue(true); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/exception/ClassCastExceptionExampleTest.java b/src/test/java/com/examplehub/basics/exception/ClassCastExceptionExampleTest.java new file mode 100644 index 00000000..1a6cbd66 --- /dev/null +++ b/src/test/java/com/examplehub/basics/exception/ClassCastExceptionExampleTest.java @@ -0,0 +1,20 @@ +package com.examplehub.basics.exception; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +import java.util.ArrayList; +import java.util.Arrays; + +class ClassCastExceptionExampleTest { + @Test + void testClassCast() { + try { + String[] strArray = new String[]{"John", "Snow"}; + ArrayList strList = (ArrayList) Arrays.asList(strArray); + System.out.println("String list: " + strList); + } catch (ClassCastException e) { + assertTrue(true); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/exception/CustomExceptionExampleTest.java b/src/test/java/com/examplehub/basics/exception/CustomExceptionExampleTest.java new file mode 100644 index 00000000..87a93d2a --- /dev/null +++ b/src/test/java/com/examplehub/basics/exception/CustomExceptionExampleTest.java @@ -0,0 +1,16 @@ +package com.examplehub.basics.exception; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CustomExceptionExampleTest { + @Test + void testCustomException() { + try { + throw new CustomExceptionExample("custom exception"); + } catch (CustomExceptionExample e) { + assertEquals("custom exception", e.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/exception/FinallyExampleTest.java b/src/test/java/com/examplehub/basics/exception/FinallyExampleTest.java new file mode 100644 index 00000000..4a177a3e --- /dev/null +++ b/src/test/java/com/examplehub/basics/exception/FinallyExampleTest.java @@ -0,0 +1,33 @@ +package com.examplehub.basics.exception; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class FinallyExampleTest { + @Test + void testWithCatch() { + int result = 0; + try { + int number = 10 / 2; + } finally { + result++; + } + assertEquals(1, result); + } + + @Test + void testReturn() { + assertEquals(3, func()); + } + public int func() { + try { + int num = 10 / 0; + return 1; + } catch (ArithmeticException e) { + return 2; + }finally { + return 3; + } + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/exception/IndexOutOfBoundsExceptionExampleTest.java b/src/test/java/com/examplehub/basics/exception/IndexOutOfBoundsExceptionExampleTest.java new file mode 100644 index 00000000..10ac1640 --- /dev/null +++ b/src/test/java/com/examplehub/basics/exception/IndexOutOfBoundsExceptionExampleTest.java @@ -0,0 +1,22 @@ +package com.examplehub.basics.exception; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class IndexOutOfBoundsExceptionExampleTest { + @Test + void testArrayAccess() { + int[] nums = {1, 2, 3, 4, 5}; + try { + int sum = 0; + for (int i = 0; i <= nums.length; ++i) { + sum += nums[i]; + } + System.out.println("sum = " + sum); + fail(); + } catch (IndexOutOfBoundsException e) { + assertTrue(true); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/exception/NullPointerExceptionExampleTest.java b/src/test/java/com/examplehub/basics/exception/NullPointerExceptionExampleTest.java new file mode 100644 index 00000000..a6ec6e72 --- /dev/null +++ b/src/test/java/com/examplehub/basics/exception/NullPointerExceptionExampleTest.java @@ -0,0 +1,18 @@ +package com.examplehub.basics.exception; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class NullPointerExceptionExampleTest { + @Test + void testNullPointer() { + try { + String str = null; + System.out.println(" str length = " + str.length()); + fail(); + } catch (NullPointerException e) { + assertTrue(true); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/exception/NumberFormatExceptionExampleTest.java b/src/test/java/com/examplehub/basics/exception/NumberFormatExceptionExampleTest.java new file mode 100644 index 00000000..55384989 --- /dev/null +++ b/src/test/java/com/examplehub/basics/exception/NumberFormatExceptionExampleTest.java @@ -0,0 +1,17 @@ +package com.examplehub.basics.exception; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class NumberFormatExceptionExampleTest { + @Test + void testFormatNumber() { + try { + int number = Integer.parseInt("123a"); + fail(); + } catch (NumberFormatException e) { + assertTrue(true); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/exception/ThrowsExceptionExampleTest.java b/src/test/java/com/examplehub/basics/exception/ThrowsExceptionExampleTest.java new file mode 100644 index 00000000..621fcedd --- /dev/null +++ b/src/test/java/com/examplehub/basics/exception/ThrowsExceptionExampleTest.java @@ -0,0 +1,39 @@ +package com.examplehub.basics.exception; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ThrowsExceptionExampleTest { + @Test + void testReceiveException() { + try { + int num = division(3, 0); + fail(); + } catch (ArithmeticException e) { + assertTrue(true); + } + } + + public int division(int a, int b) throws ArithmeticException { + return a / b; + } + + @Test + void testThrowException() { + try { + int number = parseInt("123a"); + fail(); + } catch (NumberFormatException e) { + assertTrue(true); + assertEquals("can't contains non-digits", e.getMessage()); + } + } + + public int parseInt(String str) throws NumberFormatException{ + if (!str.matches("\\d+")){ + throw new NumberFormatException("can't contains non-digits"); + } + return Integer.parseInt(str); + } +} \ No newline at end of file