write thrown exception to file

3.2k views Asked by At

is it possible to write a thrown exception to a file without using try catch blocks?

for example

public static void method() throws Exception, SQLException{
    if(Exception is thrown){
        write Exception to out.txt;
        write SQLException to out.txt;
    }
}

or do you have to do:

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

There are 7 answers

0
user unknown On BEST ANSWER

No.

public static void method() throws Exception, SQLException{
    if(Exception is thrown){
        write Exception to out.txt;
        write SQLException to out.txt;
    }
}

Where does the Exception come from?

Btw.: You never declare the most abstract exception Exception to be thrown. Some RuntimeExceptions can occur nearly everywhere (Nullpointer, OutOfMemory) so it is useless to declare them everywhere. It's just noise.

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

But if you catch it, you usually don't throw, and therefore don't declare at all to throw Exceptions.

0
hvgotcodes On

In Java, I don't think it's possible.

However, if executed on *nix, you could just redirect all output to a file (or just error output). But that's not a generic solution.

0
Amir Raminfar On

You have to catch that exception some how. If you catch it and later on decide to write it a file is ok. Given what you have above I would suggest the second way because the if-statement has no way of actually getting the exception.

0
Horatiu Jeflea On

In the second block you already catched the exception, so no need for throws Exception, SqlException.

An exception is detected on a regular line of code, so you can't add an if to each and every one. Yes, try/catch(write) is the only way.

Unless ... you use if/else instead of exception handling

0
TofuBeer On

You will need to catch the exception and rethrow it.

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;
    }
}
2
cb1 On

The only way to catch specific errors within a method is to use a Try/Catch block. However, it is possible to have a class catch all uncaught exceptions - ie anything that doesn't have a Try/Catch around it.

You can write a custom class to implement Java's UncaughtExceptionHandler interface.

import java.lang.Thread.UncaughtExceptionHandler;

public class CustomUncaughtExceptionHandler implements UncaughtExceptionHandler {
  public void uncaughtException(Thread thread, Throwable throwable) {
    /* Write to file here */
  }
}

You then need to tell the Java Virtual Machine to redirect uncaught exceptions to your class.

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

You cant do it with straight java unless you instrument the byte code (which you can do with an agent and something like asm). You can do it with an aspect, which is easier than the byte code instrumentation approach. Take aspectj for example. An aspect might be:

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 ();
    }
}

Once you weave this to your application classes, all exceptions thrown will pass through the join point here. I just printed the stack but you can log it to a file or do whatever you want. When the join point completes, the exception continues to be thrown so the calling code has a chance to handle it. Obviously, you need to change the pointcut definition to suit your classes but this is the general idea.