编码“异常驱动开发”的性能成本在Java中? [英] Performance cost of coding "exception driven development" in Java?

查看:133
本文介绍了编码“异常驱动开发”的性能成本在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在计划将异常驱动开发添加到更大的项目中,是否有通过在Java中创建,捕获和捕获异常的任何性能成本?我想设计自己的例外,并将它们包含在我的方法中,迫使开发人员抓住并做适当的工作。



例如,如果你有一个方法来获取用户从基于名称的数据库。

  public User getUser(String name); 

但是,用户可能为空,通常会忘记在此之前检查使用用户的公共方法。

 用户user = getUser(adam); 

int age = user.getAge();

这将导致NullPointerException和崩溃。但是,如果我做了一个检查,在返回用户对象之前,如果它的null并抛出一个'UserIsNullException':

  public用户getUser(String name)throws UserIsNullException; 

我强制实施者思考和采取行动:

  try {

用户user = getUser(adam);

int age = user.getAge();

} catch(UserIsNullException e){

}

它使代码对意外的崩溃更安全,并消除了更多的错误。让我们说这个网站每小时有数以百计的访问者,这个设计模式几乎遍布各地。



这样的设计方法会如何影响性能?做这个好处是要付出代价还是只是一个糟糕的代码?



感谢任何帮助!



更新!为了清楚起见,我的注意力不是包装一个NullPointerException,如我的例子所示。目标是强制实现者编写一个try / catch,从而保存一个真正的崩溃的头痛。 >

user == null



被遗忘。这个问题需要比较这两个设计模型:

  int age; 

尝试{

用户user = getUser(adam);

age = user.getAge();

} catch(UserIsNullException e){

age = 0;

}

对:

  int age; 

用户user = getUser(adam);

if(user!= null){
age = user.getAge();
} else {
age = 0;
}


解决方案


将导致
NullPointerException和崩溃。


一个NullPointerException是一个异常,可以被捕获一个catch。



所以额外的例外是多余的,除非它增加了代码的清晰度。在这种情况下,真的没有。事实上,你刚刚采取了一个未经检查的程序员错误异常,并将升级到已检查的异常。



为程序员的错误实际上意味着您的代码必须明确处理程序员引入错误的可能性,当他们没有时。


Are there are any performance cost by creating, throwing and catching exceptions in Java?

I am planing to add 'exception driven development' into a larger project. I would like to design my own exceptions and include them into my methods, forcing developers to catch and do appropriate work.

For example, if you have a method to get a user from the database based on a name.

public User getUser(String name);

However, it is possible that the user might be null and it is common to forget to check this before using a User's public method.

User user = getUser("adam");

int age = user.getAge();

which would result in NullPointerException and a crash. However, if I made a check, before returning the user-object, if its null and throw an 'UserIsNullException':

public User getUser(String name) throws UserIsNullException;

I force the implementor to think and act:

try {

    User user = getUser("adam");

    int age = user.getAge();

}catch( UserIsNullException e) {

}

It makes the code much safer for unexpected crashes and eliminates more bugs. Let say the website has hundreds of visitors per hour and this design pattern is used pretty much everywhere.

How would such a design approach effect performance? Do the benefits out-weigh the cost or is it just plain bad coding?

Thanks for any help!

UPDATE! To be clear, my attention is not wrap a NullPointerException, as my example might suggest. The goal is to force the implementer to write a try/catch, saving the head ache of a real crash since a:

user == null

was forgotten. The question concerns comparing these two design models:

int age;

try {

User user = getUser("adam");

age = user.getAge();

}catch( UserIsNullException e) {

age = 0;

}

versus:

int age;

User user = getUser("adam");

if( user != null ) {
age = user.getAge();
} else {
age = 0;
}

解决方案

"which would result in NullPointerException and a crash."

a NullPointerException is an exception, and can be caught in the a catch.

So the additional exception is redundant unless it adds clarity to the code. In this instance, it really doesn't. In fact, you've just taken an unchecked programmer error exception and promoted it to a checked exception.

Creating a checked exception for a programmer's error actually means your code has to explicitly handle the possibility that the programmer introduced an error, when they haven't.

这篇关于编码“异常驱动开发”的性能成本在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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