Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Handling Exceptions Practice | Exceptions
Java JUnit Library. Types of Testing

Handling Exceptions Practice

Свайпніть щоб показати меню

Task

Write code to handle exceptions in Java using the try-catch structure. You will work with methods that throw various exceptions. Your task is to correctly handle these exceptions in the main method.

Expected output result:

IllegalArgumentException caught: Access denied - You must be at least 18 years old.
NullPointerException caught: String value is null.
Note
Note

In this task, there won't be any unit tests because, later, you'll have to write them for this task yourself. The successful completion of this task will result in the expected output being displayed on the screen.

Hint
expand arrow

Modify the main method to handle exceptions thrown by checkAge and printLength.

Use a separate try-catch block for each method call. In each catch block, print an error message that should include the type of exception and its description.

Ensure that the program continues execution after handling the exceptions.

Solution
expand arrow
package codefinity;

public class Main {
    private static final ExceptionHandlingExercise exceptionHandlingExercise
            = new ExceptionHandlingExercise();
    public static void main(String[] args) {
        try {
            exceptionHandlingExercise.checkAge(17);
        } catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException caught: " + e.getMessage());
        }
        try {
            exceptionHandlingExercise.printLength(null);
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught: " + e.getMessage());
        }
    }
}
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Handling Exceptions Practice

Task

Write code to handle exceptions in Java using the try-catch structure. You will work with methods that throw various exceptions. Your task is to correctly handle these exceptions in the main method.

Expected output result:

IllegalArgumentException caught: Access denied - You must be at least 18 years old.
NullPointerException caught: String value is null.
Note
Note

In this task, there won't be any unit tests because, later, you'll have to write them for this task yourself. The successful completion of this task will result in the expected output being displayed on the screen.

Hint
expand arrow

Modify the main method to handle exceptions thrown by checkAge and printLength.

Use a separate try-catch block for each method call. In each catch block, print an error message that should include the type of exception and its description.

Ensure that the program continues execution after handling the exceptions.

Solution
expand arrow
package codefinity;

public class Main {
    private static final ExceptionHandlingExercise exceptionHandlingExercise
            = new ExceptionHandlingExercise();
    public static void main(String[] args) {
        try {
            exceptionHandlingExercise.checkAge(17);
        } catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException caught: " + e.getMessage());
        }
        try {
            exceptionHandlingExercise.printLength(null);
        } catch (NullPointerException e) {
            System.out.println("NullPointerException caught: " + e.getMessage());
        }
    }
}
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4
some-alt