Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.exception;

public class ArithmeticExceptionExample {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.exception;

public class ClassCastExceptionExample {
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.exception;

public class FinallyExample {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.exception;

public class IndexOutOfBoundsExceptionExample {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.exception;

public class NullPointerExceptionExample {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.exception;

public class NumberFormatExceptionExample {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.exception;

public class ThrowsExceptionExample {
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> strList = (ArrayList<String>) Arrays.asList(strArray);
System.out.println("String list: " + strList);
} catch (ClassCastException e) {
assertTrue(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}