Java 获取异常实例类名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32519410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get exception instance class name
提问by vico
I would like to know what the exception instance was in this situation:
我想知道在这种情况下异常实例是什么:
try {
// some risky actions
} catch (Exception e) {
System.out.println("Get instance name there");
}
How can I achieve this?
我怎样才能做到这一点?
回答by Abhijeet Kale
Here you go:
干得好:
try {
throw new ArithmeticException();
} catch (Exception e) {
System.out.println( e.getClass().getCanonicalName());
}
Output:
输出:
java.lang.ArithmeticException
回答by Buurman
Default exception logging is something like
默认异常日志记录类似于
try
{
//
}
catch (Exception e)
{
e.printStackTrace();
}
This will print the stacktrace of the exception to system.err
这会将异常的堆栈跟踪打印到 system.err
回答by Adriaan Koster
The type of the exception is shown as part of the output of:
异常类型显示为以下输出的一部分:
e.printStackTrace();
To get it programmatically you can use:
要以编程方式获取它,您可以使用:
String exceptionClassName = e.getClass().getName();
It is poor form to have logic depending on exception sub types within a catch block. Sonar will flag this as a code violation (squid S1193).
在 catch 块中具有依赖于异常子类型的逻辑是一种糟糕的形式。声纳会将其标记为代码违规(squid S1193)。
Instead you should add multiple catch blocks to catch different types of exceptions:
相反,您应该添加多个 catch 块来捕获不同类型的异常:
try {
// some risky actions
} catch (java.io.IOException e) {
e.printStackTrace();
}
catch (java.lang.IllegalArgumentException e) {
e.printStackTrace();
}
Since Java 7 you can also do a multi-catch:
从 Java 7 开始,您还可以执行多重捕获:
} catch (java.io.IOException | java.lang.IllegalArgumentException e) {
e.printStackTrace();
}
The benefit of the multi-catch is that you can handle multiple exception types within a single catch block without having to revert to a common super class (like java.lang.Exception
) that would include tons of exception types you didn't want to handle.
multi-catch 的好处是您可以在单个 catch 块中处理多个异常类型,而不必恢复到java.lang.Exception
包含大量您不想处理的异常类型的公共超类(如)。
回答by Sandeep Chatterjee
If you are looking to add some contextual information, you can take a look at Apache Commons ContextedRuntimeException
如果您想添加一些上下文信息,可以查看Apache Commons ContextedRuntimeException
public static void main(String[] args) {
try {
doSomething();
} catch (ContextedRuntimeException e) {
System.out.println(e.getMessage());
System.out.println(e.getContextEntries());
}
}
private static void doSomething() {
int divisor = 0;
int dividend = 100;
int result;
try {
result = dividend / divisor; // Just throw an exception to test things....
System.out.print("DIVISION RESULT: "+result);
} catch (ArithmeticException e) {
throw new ContextedRuntimeException("Oops..division by zero not allowed", e)
.addContextValue("Divisor", divisor)
.addContextValue("Dividend", dividend);
}
}
would output:
会输出:
Oops..division by zero not allowed
Exception Context:
[1:Divisor=0]
[2:Dividend=100]
---------------------------------
[(Divisor,0), (Dividend,100)]