java中最常用的运行时异常是什么? [英] What are the most commonly used runtime exceptions in java?

查看:135
本文介绍了java中最常用的运行时异常是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一名希望完善编程技巧的java程序员,我经常遇到创建运行时异常的情况。我知道如果明智地使用它是一个很好的做法。

As a java programmer who wishes to perfect his programming skills, I often come across the situations that I have to create a runtime exception. I know it's a good practice if one use wisely.

就个人而言, NullPointerException IllegalStateException 是我创建的软件中最常用的。你怎么样?

Personally, NullPointerException and IllegalStateException are the most commonly used in the softwares that I have created. How about you?

你经常使用什么运行时异常?在什么情况下你使用它们?

What runtime exceptions do you often use? In what situations do you use them?

推荐答案

我从不抛出 NullPointerException 。对我来说,当出现问题并且需要开发人员查看发生的情况时,它会在代码中自然出现。然后(s)他修复了原因并且它不再发生。

I never throw NullPointerException. For me, it is one that appears naturally in the code when something goes wrong and that requires a developer to look at what happens. Then (s)he fixes the cause and it doesn't happen again.

我使用 IllegalStateException 来表示对象配置错误或这些电话的顺序不正确。但是,我们都知道,理想情况下,对象应该确保它不会处于错误状态,并且不能以不正确的顺序调用它(构建构建器和生成的对象......)。

I use IllegalStateException to signal that an object is incorrectly configured or that calls are in an incorrect order. However, we all know that ideally, an object should ensure it can't be in a bad state and that you can't call it in incorrect order (make a builder and a resulting object ...).

当方法检测到其参数不正确时,我使用了很多 IllegalArgumentException 。这是任何公共方法的责任,停止处理(以避免更难以理解的间接错误)。此外,在方法开头的一些 if s服务于文档目的(文档从不偏离代码,因为它是代码:-))。

I use a lot of IllegalArgumentException when a method detects that its parameters are incorrect. This is the responsibility of any public method, to stop processing (to avoid indirect errors that are more difficult to understand). Also, a few ifs in the beginning of a method serve a documentation purpose (documentation that never diverge from the code because it is the code :-) ).

     public void myMethod(String message, Long id) {
       if (message == null) {
          throw new IllegalArgumentException("myMethod's message can't be null");
          // The message doesn't log the argument because we know its value, it is null.
       }
       if (id == null) {
          throw new IllegalArgumentException("myMethod's id can't be null");
          // This case is separated from the previous one for two reasons :
          // 1. to output a precise message
          // 2. to document clearly in the code the requirements
       }
       if (message.length()<12) {
          throw new IllegalArgumentException("myMethod's message is too small, was '" + message + "'");
          // here, we need to output the message itself, 
          // because it is a useful debug information.
       }
     }

我还使用特定的运行时异常表示更高级别的异常情况。

I also use specific Runtime Exceptions to signal higher level exceptional conditions.


例如,如果我的应用程序的模块无法启动,我可能会抛出 ModuleNotOperationalException (理想情况下,当另一个模块调用它时,通过类似拦截器的通用代码,或者通过特定代码)。在该架构决策之后,每个模块必须在调用其他模块的操作上处理此异常...

For example, if a module of my application couldn't start, I might have a ModuleNotOperationalException thrown (ideally by a generic code like an interceptor, otherwise by a specific code) when another module calls it. After that architectural decision, each module has to deal with this exception on operations that call other modules...

这篇关于java中最常用的运行时异常是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆