Home > Java > javaTutorial > body text

Creation and use of Java custom exceptions

WBOY
Release: 2024-05-03 22:27:01
Original
1102 people have browsed it

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 {
    // ...
}
Copy after login

Set exception message

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

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

Throw Exception

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

throw new MyCustomException("Custom exception message");
Copy after login

Using a custom exception

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

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

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);
    }
}
Copy after login

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);
    }
}
Copy after login

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());
    }
}
Copy after login

Output:

Invalid input: abc
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template