尝试将异常输出到文件 - 如何将对象传递到 catch 块中?
Trying to ouput exceptions to a file - how can I pass objects into catch block?
我有这样一种情况,我试图处理 table 中的大量行,并将结果(成功或异常)输出到 excel 传播sheet 使用jxl。问题是我看不到如何将启动的 Excel 工作簿传递给 catch 块,以便可以将异常打印到它。
下面是一些简化的代码,概述了我的程序结构(忽略任何基本错误,我只是为了演示目的而输入):
public static void main(String[] args) {
try{
WritableWorkbook exceptionWB=Workbook.createWorkbook(new File("filename.xls");
WritableSheet excepSheet=exceptionWB.createSheet("Exceptions",0);
int exceptionRow=1;
for(int i=0; i<numberOfRecords;i++){
processRow(argument1, argument2, excepSheet, exceptionRow);
}
catch (Exception e){
Label exception1=new Label(0, exceptionRow, e.getMessage());
exceptionWB.write();
}
}
public void processRow(String arg1, String arg2, WritableSheet sheet, int row){
try{
// A load of operations here
} catch(Exception e) {
Label exception1=new Label(0, exceptionRow, e.getMessage());
exceptionWB.write();
}
}
我知道 catch 块无法访问 sheet,因为它不在范围内,但是如果我将工作簿启动移出 try 块,它会返回错误,因为没有异常处理它。
有没有一种方法可以将此 WorkBook/Sheet 传递到各个 catch 块,或者我应该寻找一个完全不同的解决方案?
谢谢
定义如下:
public static void main(String[] args) {
WritableWorkbook exceptionWB = null;
try{
exceptionWB = Workbook.createWorkbook(new File("filename.xls");
WritableSheet excepSheet=exceptionWB.createSheet("Exceptions",0);
int exceptionRow=1;
for(int i=0; i<numberOfRecords;i++) {
processRow(argument1, argument2, excepSheet, exceptionRow);
}
catch (Exception e) {
//You should now have access to the exceptionWB
Label exception1=new Label(0, exceptionRow, e.getMessage());
exceptionWB.write();
}
}
我有这样一种情况,我试图处理 table 中的大量行,并将结果(成功或异常)输出到 excel 传播sheet 使用jxl。问题是我看不到如何将启动的 Excel 工作簿传递给 catch 块,以便可以将异常打印到它。 下面是一些简化的代码,概述了我的程序结构(忽略任何基本错误,我只是为了演示目的而输入):
public static void main(String[] args) {
try{
WritableWorkbook exceptionWB=Workbook.createWorkbook(new File("filename.xls");
WritableSheet excepSheet=exceptionWB.createSheet("Exceptions",0);
int exceptionRow=1;
for(int i=0; i<numberOfRecords;i++){
processRow(argument1, argument2, excepSheet, exceptionRow);
}
catch (Exception e){
Label exception1=new Label(0, exceptionRow, e.getMessage());
exceptionWB.write();
}
}
public void processRow(String arg1, String arg2, WritableSheet sheet, int row){
try{
// A load of operations here
} catch(Exception e) {
Label exception1=new Label(0, exceptionRow, e.getMessage());
exceptionWB.write();
}
}
我知道 catch 块无法访问 sheet,因为它不在范围内,但是如果我将工作簿启动移出 try 块,它会返回错误,因为没有异常处理它。
有没有一种方法可以将此 WorkBook/Sheet 传递到各个 catch 块,或者我应该寻找一个完全不同的解决方案?
谢谢
定义如下:
public static void main(String[] args) {
WritableWorkbook exceptionWB = null;
try{
exceptionWB = Workbook.createWorkbook(new File("filename.xls");
WritableSheet excepSheet=exceptionWB.createSheet("Exceptions",0);
int exceptionRow=1;
for(int i=0; i<numberOfRecords;i++) {
processRow(argument1, argument2, excepSheet, exceptionRow);
}
catch (Exception e) {
//You should now have access to the exceptionWB
Label exception1=new Label(0, exceptionRow, e.getMessage());
exceptionWB.write();
}
}