Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to check the parameters

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to check the parameters". Friends who are interested may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to check the parameters.

Background

Most methods and constructors have some restrictions on the passed-in parameter values, such as: common index values must be non-negative and object references cannot be empty.

You should use clear documentation to mark all these limitations and then force them to check at the beginning of the method body.

It is a basic principle that mistakes should be detected as soon as possible.

If you don't, the error will not be detected when it occurs, which makes it more difficult to locate the source of the error.

If an illegal parameter is passed to a method, a parameter check is performed before the method is executed. It will fail quickly and give clear exception information.

If the method does not check the parameters, the following things will happen.

Degree description

Bad methods fail during execution and throw an ambiguous exception

The worse method returns normally, but quietly calculates an incorrect value.

The worst method returns normally, but some objects are in an incorrect state, and an uncertain point in time in the future will cause an error at some unrelated point.

In a word: failure to check parameters will lead to atomicity failure.

Recommended practice

For public and protected methods, use the @ throws tag of the java document to mark the exception that will be thrown if the parameter value is illegal.

Common exception types of parameter checking are as follows:

As long as you have marked the limits of the method parameters and the exceptions that will be thrown if you violate the restrictions, the restrictions will be a simple thing. Here is a typical example.

/ * * @ param m must be a positive integer * @ throws ArithmeticException if m

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report