Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java .getStackTrace()[2]。getClassName()+“+”+线程。currentThread().getStackTrace()[2]。getMethodName()+“+”+线程。currentThread().getStackTrace()[1]。getMethodName()+“:无效参数:列表为空”); 地址(地址a:地址) 如果(a==null) 抛出新异常(Thread.currentThread().getStackTrace()[2].getClassName()+“+Thread.currentThread().getStackTrace()[2].getMethodName()+“+Thread.currentThread().getStackTrace()[1].getMethodName()+”:无效列表项:对象为空); } }_Java_Exception - Fatal编程技术网

Java .getStackTrace()[2]。getClassName()+“+”+线程。currentThread().getStackTrace()[2]。getMethodName()+“+”+线程。currentThread().getStackTrace()[1]。getMethodName()+“:无效参数:列表为空”); 地址(地址a:地址) 如果(a==null) 抛出新异常(Thread.currentThread().getStackTrace()[2].getClassName()+“+Thread.currentThread().getStackTrace()[2].getMethodName()+“+Thread.currentThread().getStackTrace()[1].getMethodName()+”:无效列表项:对象为空); } }

Java .getStackTrace()[2]。getClassName()+“+”+线程。currentThread().getStackTrace()[2]。getMethodName()+“+”+线程。currentThread().getStackTrace()[1]。getMethodName()+“:无效参数:列表为空”); 地址(地址a:地址) 如果(a==null) 抛出新异常(Thread.currentThread().getStackTrace()[2].getClassName()+“+Thread.currentThread().getStackTrace()[2].getMethodName()+“+Thread.currentThread().getStackTrace()[1].getMethodName()+”:无效列表项:对象为空); } },java,exception,Java,Exception,关于线程的那些东西是什么?疯狂的代码 首先,不要允许任何人将空实例添加到列表中 我可以这样写: public void checkAddresses(List<Address> addresses) { if (addresses == null) throw new IllegalArgumentException("Address List cannot be null"); for (Address a : addresses) { if (a

关于线程的那些东西是什么?疯狂的代码

首先,不要允许任何人将空实例添加到列表中

我可以这样写:

public void checkAddresses(List<Address> addresses) {
    if (addresses == null) throw new IllegalArgumentException("Address List cannot be null");
    for (Address a : addresses) {
        if (a == null) throw new IllegalArgumentException("Address cannot be null");
    }
}
公共无效检查地址(列表地址){
如果(addresses==null)抛出新的IllegalArgumentException(“地址列表不能为null”);
地址(地址a:地址){
如果(a==null)抛出新的IllegalArgumentException(“地址不能为null”);
}
}

打印堆栈跟踪不会处理异常。如果您希望该方法抛出异常,为什么要将其包装在try/catch块中?@duffymo我同意您的看法-这就是为什么我不鼓励使用
printStackTrace()
。我不知道你反对什么
.printStackTrace()
通常(不幸的是)在用户尝试将try-catch块添加为catch主体时由IDE自动添加。打印堆栈跟踪不会处理异常。如果您希望该方法抛出异常,为什么要将其包装在try/catch块中?@duffymo我同意您的看法-这就是为什么我不鼓励使用
printStackTrace()
。我不知道你反对什么
.printStackTrace()
通常是(不幸的)在用户尝试添加一个try-catch块作为捕获的主体时由IDE自动添加的。您甚至可以执行
地址.stream().anyMatch(Objects::isNull)
。您甚至可以执行
地址.stream().anyMatch(Objects::isNull)
。我知道您说过这是示例代码,但我觉得有必要指出,整个
getStackTrace()[n]
的想法都不好,不仅是因为可读性和性能原因,而且因为JVM被允许删除堆栈帧。更好的做法是将类名和方法名作为字符串参数传递,如。如果没有getStackTrace(),我如何才能获取要传递给记录器的当前类和方法的名称?checkAddress不应尝试获取该信息。调用者应该将它们作为参数传递。就像我链接到的Logger方法一样。我知道你说这是示例代码,但我觉得有必要指出,整个
getStackTrace()[n]
方法是个坏主意,不仅是因为可读性和性能原因,而且因为JVM被允许删除堆栈帧。更好的做法是将类名和方法名作为字符串参数传递,如。如果没有getStackTrace(),我如何才能获取要传递给记录器的当前类和方法的名称?checkAddress不应尝试获取该信息。调用者应该将它们作为参数传递。就像我链接到的Logger方法一样,我使用getStackTrace()向自己指示发生错误的包/类/方法。同一方法在代码中被多次调用,如果出现错误,您需要快速找到发生此错误的位置。打印这样一个stig会给出一个错误的方法的绝对路径。你不需要所有的噪音。这是一个未经检查的异常。我建议你让打电话的人捕捉异常情况,并告诉你发生了什么。address方法不应以这种方式提供上下文。我写的时候,它不需要知道它的名字。明白了,谢谢!还有一个问题-异常何时起作用,您建议通过前端?默认情况下,发送错误代码500,是否值得传递异常文本,或者是不同的音调,并且只需要处理错误500?否,异常不应传播到客户端。他们关心你的NullPointerException什么?我使用getStackTrace()向自己指出发生错误的包/类/方法。同一方法在代码中被多次调用,如果出现错误,您需要快速找到发生此错误的位置。打印这样一个stig会给出一个错误的方法的绝对路径。你不需要所有的噪音。这是一个未经检查的异常。我建议你让打电话的人捕捉异常情况,并告诉你发生了什么。address方法不应以这种方式提供上下文。我写的时候,它不需要知道它的名字。明白了,谢谢!还有一个问题-异常何时起作用,您建议通过前端?默认情况下,发送错误代码500,是否值得传递异常文本,或者是不同的音调,并且只需要处理错误500?否,异常不应传播到客户端。他们关心你的NullPointerException什么?它应该被翻译成用户友好的信息。
e.printStackTrace();
} catch(Exception e) {
    e.printStackTrace();
}
} catch(Exception e) {
    e.printStackTrace();
    throw e; // <-- re-throw the Exception
}
public void checkAddress(List<Address> addresses) throws Exception {
    if (addresses == null) {
        StackTraceElement[] ste = Thread.currentThread().getStackTrace();
        throw new Exception(ste[2].getClassName() + "."
                + ste[2].getMethodName() + "." + ste[1].getMethodName()
                + ": Invalid parameter: list is null");
    }
    if (addresses.stream().anyMatch(a -> a == null)) {
        StackTraceElement[] ste = Thread.currentThread().getStackTrace();
        throw new Exception(ste[2].getClassName() + "."
                + ste[2].getMethodName() + "." + ste[1].getMethodName()
                + ": Invalid list item: object is null");
    }
}
public void checkAddress(List<Address> addresses) throws Exception{
        if(addresses == null)
            throw new Exception(Thread.currentThread().getStackTrace()[2].getClassName() + "." + Thread.currentThread().getStackTrace()[2].getMethodName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName() + ": Invalid parameter: list is null");
        for(Address a : addresses)
            if(a == null)
                throw new Exception(Thread.currentThread().getStackTrace()[2].getClassName() + "." + Thread.currentThread().getStackTrace()[2].getMethodName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName() + ": Invalid list item: object is null");
    }

}
public void checkAddresses(List<Address> addresses) {
    if (addresses == null) throw new IllegalArgumentException("Address List cannot be null");
    for (Address a : addresses) {
        if (a == null) throw new IllegalArgumentException("Address cannot be null");
    }
}