Home  >  Article  >  Java  >  Creation and use of Java custom exceptions

Creation and use of Java custom exceptions

WBOY
WBOYOriginal
2024-05-03 22:27:011104browse

Custom exceptions are used to create error messages and processing logic. First, you need to inherit Exception or RuntimeException to create a custom exception class. You can then override the getMessage() method to set the exception message. Exceptions are thrown using the throw keyword. Use try-catch blocks to handle custom exceptions. This article provides a practical case for parsing integer input and throwing a custom InvalidInputException exception when the input is not an integer.

Creation and use of Java custom exceptions

Creation and use of Java custom exceptions

Introduction

Customization Exceptions allow developers to create custom error messages and exception handling logic. In this article, we will introduce how to create and use Java custom exceptions and provide a practical example.

Create custom exception

To create a custom exception class, you need to extend the Exception or RuntimeException class:

public class MyCustomException extends Exception {
    // ...
}

Set exception message

You can override the getMessage() method to customize the exception message:

@Override
public String getMessage() {
    return "Custom exception message";
}

Throw Exception

You can throw a custom exception by using the throw keyword:

throw new MyCustomException("Custom exception message");

Using a custom exception

You can use the try-catch block to handle custom exceptions:

try {
    // 代码可能引发 MyCustomException
} catch (MyCustomException e) {
    // 处理 MyCustomException
}

Practical case

Suppose we have a method to handle user input of integers and want to throw a custom exception when the input is not an integer. We can use the following custom exception:

public class InvalidInputException extends Exception {
    public InvalidInputException(String message) {
        super(message);
    }
}

In the method that handles integer input, we can throw InvalidInputException:

public int parseInteger(String input) {
    try {
        return Integer.parseInt(input);
    } catch (NumberFormatException e) {
        throw new InvalidInputException("Invalid input: " + input);
    }
}

In the main method, we call parseInteger() method and handle InvalidInputException:

public static void main(String[] args) {
    try {
        int number = parseInteger("abc");
    } catch (InvalidInputException e) {
        System.out.println(e.getMessage());
    }
}

Output:

Invalid input: abc

The above is the detailed content of Creation and use of Java custom exceptions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn