Java异常处理获取控制台错误消息
我想在生成异常时使用java获取错误消息 现在我有了具有以下场景的java代码:Java异常处理获取控制台错误消息,java,exception-handling,Java,Exception Handling,我想在生成异常时使用java获取错误消息 现在我有了具有以下场景的java代码: method first(){ try{ second(); }catch(Exception e){ System.out.println("Error:> "+e) } } 现在,当第一个方法执行时,我只得到“Exception generate in second method”消息,但控制台上有java打印的一些其他消息,所以如何获
method first(){
try{
second();
}catch(Exception e){
System.out.println("Error:> "+e)
}
}
现在,当第一个方法执行时,我只得到“Exception generate in second method”消息,但控制台上有java打印的一些其他消息,所以如何获得控制台错误消息
注意:我已经尝试使用e.getMessage();和e.printStackTrace() 异常构造函数参数中的消息不会打印在异常详细信息中。 您只需使用以下代码打印消息:
method first(){
try{
second();
}catch(Exception e){
System.out.println("Error:> "+e.getMessage())
}
}
希望这能解决您的问题为什么不能使用打印堆栈跟踪? 因为throwable包含创建时线程执行堆栈的快照。(请参阅) 这意味着,如果要打印堆栈跟踪,则需要使用
printStackTrace()
方法,但要使用第二种方法
method second(){
try {
my code
} catch(Exception e) {
e.printStackTrace();
throw new Exception("Exception generate in second method",e);
}
}
或者先使用棘手的方法setStackTrace
并使用printStackTrace()
method second(){
try {
my code
} catch(Exception e) {
Exception ex = new Exception("Exception generate in second method",e);
ex.setStackTrace(e);
throw ex;
}
}
您可以打印得到的异常的原因。试试这个:
method first(){
try{
second();
}catch(Exception e){
System.out.println("Error:> "+e);
if (e.getCause() != null) {
System.out.println("Cause:> " + e.getCause());
}
}
}
每个异常都有一个原因,您可以通过
getCause()
获得该原因。你可以循序渐进,直到找到根本原因。下面是一个实用程序的示例,它像控制台一样转储异常及其所有原因
private void first() {
try {
second();
} catch (Exception ex) {
Log.e("CATCH", getExceptionDump(ex));
}
}
private void second() {
try {
throw new UnsupportedOperationException("We don't do this.");
} catch (Exception ex) {
throw new RuntimeException("Exception in second()", ex);
}
}
private String getExceptionDump(Exception ex) {
StringBuilder result = new StringBuilder();
for (Throwable cause = ex; cause != null; cause = cause.getCause()) {
if (result.length() > 0)
result.append("Caused by: ");
result.append(cause.getClass().getName());
result.append(": ");
result.append(cause.getMessage());
result.append("\n");
for (StackTraceElement element: cause.getStackTrace()) {
result.append("\tat ");
result.append(element.getMethodName());
result.append("(");
result.append(element.getFileName());
result.append(":");
result.append(element.getLineNumber());
result.append(")\n");
}
}
return result.toString();
}
我相信这是你想要达到的安慰信息:
Error:> java.lang.Exception: Exception generate in second method
试试这段代码,当第二个方法的catch块抛出异常时,第二个方法应该将其声明为throws,或者在catch块中放入嵌套的Try-catch
异常被传播到由其catch块处理的first()方法
public class Test {
public void first() {
try {
second();
} catch (Exception e) {
System.out.println("Error:> " + e);
}
}
public void second() throws Exception {
try {
throw new Exception();
} catch (Exception e) {
throw new Exception("Exception generate in second method", e);
}
}
public static void main(String ars[]) {
Test test = new Test();
test.first();
}
}
您希望在哪里打印堆栈?在第一个或第二个方法中?您可以将消息添加到您的问题中吗?这里您得到的“Exception generate In second method”只是因为您在second()方法中捕获异常,因此在first()中捕获块方法将永远不会执行。@ShoaibChikate第二个方法中的catch块引发异常,因此它将传播到调用方法i;e第一种方法。理想情况下,第一个方法的catch块应该run@underdog当前位置我的错误,我没有看到我的道歉。
Error:> java.lang.Exception: Exception generate in second method
public class Test {
public void first() {
try {
second();
} catch (Exception e) {
System.out.println("Error:> " + e);
}
}
public void second() throws Exception {
try {
throw new Exception();
} catch (Exception e) {
throw new Exception("Exception generate in second method", e);
}
}
public static void main(String ars[]) {
Test test = new Test();
test.first();
}
}