Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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 将引发的异常写入文件_Java_Exception_Exception Handling_Throws - Fatal编程技术网

Java 将引发的异常写入文件

Java 将引发的异常写入文件,java,exception,exception-handling,throws,Java,Exception,Exception Handling,Throws,是否可以在不使用try-catch块的情况下将抛出的异常写入文件 比如说 public static void method() throws Exception, SQLException{ if(Exception is thrown){ write Exception to out.txt; write SQLException to out.txt; } } 或者你必须做: public static void method(){

是否可以在不使用try-catch块的情况下将抛出的异常写入文件

比如说

public static void method() throws Exception, SQLException{
    if(Exception is thrown){
        write Exception to out.txt;
        write SQLException to out.txt;
    }
}
或者你必须做:

public static void method(){
    try{
        ...
    }catch(Exception ex){
        write ex to file;
    }catch(SQLException sqlex){
        write sqlex to file;
    }
}
在Java中,我认为这是不可能的

但是,如果在*nix上执行,则可能只是错误输出。但这不是一个通用的解决方案。

在Java中,我认为这是不可能的

但是,如果在*nix上执行,则可能只是错误输出。但这不是一个通用的解决方案。

您必须以某种方式捕获该异常。如果你捕捉到它,然后决定写一个文件就可以了。考虑到上面的内容,我建议使用第二种方法,因为if语句实际上无法获得异常

你必须以某种方式抓住这个例外。如果你捕捉到它,然后决定写一个文件就可以了。考虑到上面的内容,我建议使用第二种方法,因为if语句实际上无法获得异常

没有

public static void method() throws Exception, SQLException{
    if(Exception is thrown){
        write Exception to out.txt;
        write SQLException to out.txt;
    }
}
例外从何而来

顺便说一句:您从未声明要抛出的最抽象的异常。有些运行时异常几乎可以发生在Nullpointer、OutOfMemory的任何地方,所以在任何地方声明它们都是无用的。只是噪音

public static void method() throws Exception, SQLException{
    try{
        ...
    }catch(Exception ex){
        write ex to file;
    }catch(SQLException sqlex){
        write sqlex to file;
    }
}
但是如果您捕获了它,通常不会抛出异常,因此根本不会声明抛出异常。

public static void method() throws Exception, SQLException{
    if(Exception is thrown){
        write Exception to out.txt;
        write SQLException to out.txt;
    }
}
例外从何而来

顺便说一句:您从未声明要抛出的最抽象的异常。有些运行时异常几乎可以发生在Nullpointer、OutOfMemory的任何地方,所以在任何地方声明它们都是无用的。只是噪音

public static void method() throws Exception, SQLException{
    try{
        ...
    }catch(Exception ex){
        write ex to file;
    }catch(SQLException sqlex){
        write sqlex to file;
    }
}

但是如果捕获到它,通常不会抛出异常,因此根本不会声明抛出异常。

在第二个块中,您已经捕获了异常,因此不需要抛出异常,SqlException

在常规代码行中检测到异常,因此不能向每一行添加if。是的,尝试/写是唯一的方法


除非。。。在已经捕获异常的第二个块中,使用if/else代替异常处理,因此不需要抛出异常SqlException

在常规代码行中检测到异常,因此不能向每一行添加if。是的,尝试/写是唯一的方法


除非。。。如果使用if/else而不是异常处理

,则需要捕获异常并重新播放它

public static void method() throws Exception, SQLException{
    try{
        ...
    }
    catch(SQLException sqlex){
        write sqlex to file;
        throw ex;
    } 
    catch(Exception ex){
        write ex to file;
        throw ex;
    }
}

您需要捕获异常并重新播放它

public static void method() throws Exception, SQLException{
    try{
        ...
    }
    catch(SQLException sqlex){
        write sqlex to file;
        throw ex;
    } 
    catch(Exception ex){
        write ex to file;
        throw ex;
    }
}

捕捉方法中特定错误的唯一方法是使用Try/catch块。然而,有可能让一个类捕获所有未捕获的异常——即任何没有Try/catch的异常

您可以编写一个自定义类来实现Java的UncaughtExceptionHandler接口

import java.lang.Thread.UncaughtExceptionHandler;

public class CustomUncaughtExceptionHandler implements UncaughtExceptionHandler {
  public void uncaughtException(Thread thread, Throwable throwable) {
    /* Write to file here */
  }
}
然后,您需要告诉Java虚拟机将未捕获的异常重定向到您的类

public static void main(String[] args){
  Thread.setDefaultUncaughtExceptionHandler(new CustomUncaughtExceptionHandler());
}

捕捉方法中特定错误的唯一方法是使用Try/catch块。然而,有可能让一个类捕获所有未捕获的异常——即任何没有Try/catch的异常

您可以编写一个自定义类来实现Java的UncaughtExceptionHandler接口

import java.lang.Thread.UncaughtExceptionHandler;

public class CustomUncaughtExceptionHandler implements UncaughtExceptionHandler {
  public void uncaughtException(Thread thread, Throwable throwable) {
    /* Write to file here */
  }
}
然后,您需要告诉Java虚拟机将未捕获的异常重定向到您的类

public static void main(String[] args){
  Thread.setDefaultUncaughtExceptionHandler(new CustomUncaughtExceptionHandler());
}

除非插入字节码,否则不能用纯java实现,而字节码可以用代理之类的工具实现。您可以使用方面来完成,这比字节码插装方法更简单。比如说。一个方面可能是:

public aspect ExceptionAdvice {

    pointcut allMethods() : execution(* *(..)) && !within(com.test.ExceptionAdvice);

    after() throwing(Exception e) : allMethods() {
        //write to a file or whatever - I'm lazy and just printed the stack
        e.printStackTrace ();
    }
}

一旦将其编织到应用程序类中,抛出的所有异常都将通过此处的连接点。我刚刚打印了堆栈,但您可以将其记录到文件或执行任何您想要的操作。当连接点完成时,将继续抛出异常,以便调用代码有机会处理它。显然,您需要更改切入点定义以适合您的类,但这是一般的想法。

除非您插入字节码(您可以使用代理或类似工具来完成),否则无法使用纯java来完成此操作。您可以使用方面来完成,这比字节码插装方法更简单。比如说。一个方面可能是:

public aspect ExceptionAdvice {

    pointcut allMethods() : execution(* *(..)) && !within(com.test.ExceptionAdvice);

    after() throwing(Exception e) : allMethods() {
        //write to a file or whatever - I'm lazy and just printed the stack
        e.printStackTrace ();
    }
}

一旦将其编织到应用程序类中,抛出的所有异常都将通过此处的连接点。我刚刚打印了堆栈,但您可以将其记录到文件或执行任何您想要的操作。当连接点完成时,将继续抛出异常,以便调用代码有机会处理它。显然,您需要更改切入点定义以适合您的类,但这是一般的想法。

您必须喜欢AOP来处理这样的事情!你必须热爱AOP来处理这样的事情!你在做这件事的时候要小心,因为你会弄坏很多东西。我认为你不想使用这种方法。要到达这个处理程序,如果您想尝试抛出任何异常,您必须让异常未捕获所有异常未捕获。如果你
如果你想在文件中写入一个异常并处理异常情况,那你就不走运了。在这样做的时候你应该小心,因为你可能会破坏很多东西。我认为你不想使用这种方法。要到达这个处理程序,如果您想尝试抛出任何异常,您必须让异常未捕获所有异常未捕获。如果您既想向文件写入异常,又想处理异常条件,那么您就不走运了。