English 中文(简体)
在 Java设立例外原因
原标题:Setting an exception cause in Java

我可以看到一个例外,我可以打印<代码>e.getCause(,但总是null

难道我需要把它放在某个地方,还是遗漏了什么东西,什么是造成失败的原因?

最佳回答

属性<编码>message和cause。 该信息是一种描述,更确切地说是,什么是错的。

The concept is often used if we use custom exceptions like this:

catch(IOException e) {
  throw new ApplicationException("Failed on reading file soandso", e);
  //                              ^ Message                        ^ Cause
}

答复 s comment:

The standard is that the nested expression (the cause) is printed with its stack trace too.

很少应用

public class Exceptions {
    public static void main(String[] args) {
        Exception r = new RuntimeException("Some message");
        throw new RuntimeException("Some other message", r);
    }
}

产出

Exception in thread "main" java.lang.RuntimeException: Some other message
    at Exceptions.main(Exceptions.java:4)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.RuntimeException: Some message
    at Exceptions.main(Exceptions.java:3)
    ... 5 more

Both messages are included.

问题回答

原因通常在例外情况的构造中确定。 看见public Statelessness(String information, Throwable causes)

如果在建筑商中固定,请打电话initCause()。

<+>Exception有条码的构造。 可浏览。 你们需要把这些建筑商或建筑商称作超级建筑商的习俗例外类别。

If you just want to get rid of null as the cause then override toString() method of your CustomException class that extends Exception class.

public class CustomException extends Exception {    
    private static final long serialVersionUID = 9355648L;
    public CustomException(String message, Throwable cause) {
        super(message, cause);
    }
    @Override
    public String toString() {
        return "Business Exception";
    }
}

以及

throw new CustomException("Stopping further processing.", new CustomException("stale message"));

产出如下:

Stopping further processing. Cause : Business Exception

a 包括:

} catch (CustomException ce) {
    System.out.print(ce.getMessage());
    System.out.println(" Cause : " + ce.getCause());
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签