Code Review

Exception handling

?

?

public class CarsDriveException extends RuntimeException{
    public CarsDriveException(String message) {
        super(message);
    }
}

?

public class Task01 {
    public static String getSubstring(String str) {
        return str.substring(5);
    }
}

?

public class Task03 {
    public static String getIsPositiveNumber(int number) throws IsPositiveNumberException {
        final String POSITIVE_NUMBER = "This number is positive";
        if (number > 0) {
            return POSITIVE_NUMBER;
        } else {
            throw new IsPositiveNumberException("This number is negative");
        }
    }
}

?

public class Task03Demo {
    public static void main(String[] args) throws IsPositiveNumberException {
        String num;

        try {
            num = Task03.getIsPositiveNumber(-5);
        } catch (IsPositiveNumberException e) {
            num = e.getMessage();
            e.printStackTrace();
        }
        System.out.println(num);
    }
}

?

public class Task04 {
    private String message;

    public Task04(String message) {
        this.message = message;
    }
    public static String getCarCapacity(int weightOfCar) throws CarsDriveException {
        final int MAX_WEIGHT_CAR = 1000;
        final String CAN_DRIVE = "We are driving!";
        if(weightOfCar < MAX_WEIGHT_CAR) {
            return CAN_DRIVE;
        } else {
            throw new CarsDriveException("We cannot drive! The car is overloaded.");
        }
    }
}

?

public class Task05 {
    public static String getCharAt(String str) throws EmptyStringException {
        int numberChar = 0;
        try {
            if (str == null) {
                throw new NullPointerException();
            }
        } catch (NullPointerException e) {
            throw new EmptyStringException("You strings is empty");
        }
        return "The char on this position is:" + String.valueOf(str.charAt(numberChar));
    }
}

?

ublic class Task06 {
    public static void randomException() throws Exception {
    Random random = new Random();
    int randomException = random.nextInt(3);
    switch (randomException) {
        case 0:
            throw new NullPointerException();
        case 1:
            throw new ArrayIndexOutOfBoundsException();
        default:
            throw new NoSuchMethodException();
    }
    }
}

?

public class Task06Demo {
    public static void main(String[] args) throws Exception {
        String randomResult;

        try {
            Task06.randomException();
        } catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            System.out.println(e.getMessage());
        }
    }
}

?

public class Task07 {
    public static void throwExceptionMultiple() {
        int number = (int) (Math.random() * 3);
        if (number == 0) {
            throw new NullPointerException();
        }
    }
}

?

public boolean callMyCustomRuntimeException(int Age) {
    if (Age < 18) {
        throw new CustomRuntimeException("You can't buy alcohol");
    }
    return true;
}

?

public class DemoTask03 {
    public static void main(String[] args) {
        Tasks tasks = new Tasks();
        try {
            tasks.callDegreeOfAlcoholValidation(31);
        } catch (DegreeOfAlcoholValidation ex) {
            System.out.println("stack trace");
        }
    }
}

?

public class CallNullPointerException {
    public void stringConcat() {
        String string = null;
        string = string.concat("");
    }
}

?

public class ExceptionClass extends Exception {
    public ExceptionClass(String errorMessage) {
        super(errorMessage);
    }
}

?

public class ExceptionSimulationsTest {
    ExceptionSimulations tasks;

    @BeforeEach
    public void instanceTasks() {
        tasks = new ExceptionSimulations();
    }

    @Test
    public void callNullPointerExceptionTest() {
        Executable executable = () -> tasks.callNullPointerException();

        Assertions.assertThrows(NullPointerException.class, executable);
    }

    @Test
    public void callArrayIndexOutOfBoundsExceptionTest() {
        Executable executable = () -> tasks.callArrayIndexOutOfBoundsException();

        Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, executable);
    }

    @Test
    public void callCustomExceptionTest() {
        Executable executable = () -> tasks.callCustomException();

        Assertions.assertThrows(CustomException.class, executable);
        System.out.println("stack trace");
    }

    @Test
    public void callCustomRuntimeExceptionTest() {
        Executable executable = () -> tasks.callCustomRuntimeException();

        Assertions.assertThrows(CustomRuntimeException.class, executable);
    }

    @Test
    public void callIllegalAccessExceptionTest() {
        Executable executable = () -> tasks.callIllegalAccessException();

        Assertions.assertThrows(IllegalAccessException.class, executable);
    }

    @Test
    public void callThreeExceptionsTest() {
        Executable executableLessZero = () -> tasks.callThreeExceptions(-1);
        Executable executableIsZero = () -> tasks.callThreeExceptions(-1);
        Executable executableMoreZero = () -> tasks.callThreeExceptions(1);

        Assertions.assertThrows(NumberIsNotValidException.class, executableLessZero);
        Assertions.assertThrows(NumberIsNotValidException.class, executableIsZero);
        Assertions.assertThrows(NumberIsValidException.class, executableMoreZero);
    }
}