所以我有以下抛出IOException的方法:
public static void saveObjectToTextFile(String string,
String textFileName) throws
IOException {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File(textFileName)));
bw.write(string);
} finally {
try {
bw.close();
} catch (Exception e) {
}
}
}
我的问题是,无论何时我编写另一个调用上述函数的方法,我最终都必须在方法头中编写throws IOException
,如下所示:
void anotherMethod() throws IOException {
FileInputOutput.saveObjectToTextFile(JSONobject,
finalPathAndFile);
}
如果不为throws IOException
撰写anotherMethod()
,我该怎么做?
答案 0 :(得分:2)
如何实现这一点取决于当时获得异常时的含义。是否可以通过此代码块完成某些操作?如果是这样,你在这里处理它并继续
public static void saveObjectToTextFile(String string,
String textFileName){ // no throws
try (BufferedWriter bw = new BufferedWriter(new FileWriter(new File(textFileName)))) { // use try-with-resources
bw.write(string);
} catch(IOException e){
// deal with the exception
} // no close necessary for try-with-resources
}
如果没有这个代码块可以处理异常,请将其包装在RuntimeException
public static void saveObjectToTextFile(String string,
String textFileName){ // no throws
try (BufferedWriter bw = new BufferedWriter(new FileWriter(new File(textFileName)))) { // use try-with-resources
bw.write(string);
} catch(IOException e){
throw new IllegalStateException("Cannot write the string to " + textFileName,e);
} // no close necessary for try-with-resources
}
要明确:“处理它”通常意味着做一些建设性的事情,除了记录,这会使应用程序处于可接受的状态。如果应用程序希望此方法成功将字符串写入文件,则应该选择后一种实现。
答案 1 :(得分:0)
您只需捕获 IOException :
public static void saveObjectToTextFile(String string,
String textFileName)
{
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File(textFileName)));
bw.write(string);
} catch(IOException e) {
//Handle the Exception
} finally {
bw.close();
}
}
关闭方法根本不会抛出任何异常。 finally块用于关闭或重置变量或对象的实例。
答案 2 :(得分:0)
如果你不想进一步抛出异常,你必须在方法中捕获它,它将如下所示:
void anotherMethod() {
try {
FileInputOutput.saveObjectToTextFile(JSONobject,
finalPathAndFile);
} catch (final IOException e) {
// do something to handle the failure
}
}
您可以在此处详细了解:https://docs.oracle.com/javase/tutorial/essential/exceptions/
答案 3 :(得分:0)
您可以按照以下方式重写方法saveObjectToTextFile
,这样您将在方法本身中捕获异常,并且不会进一步传播。
public static void saveObjectToTextFile(String string,
String textFileName) throws
IOException {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File(textFileName)));
bw.write(string);
} catch (Exception e) {
//do whatever you need to do with an exception for example to print it
e.printStackTrace(); //print an exception to console
}finally {
try {
bw.close();
} catch (Exception e) {
e.printStackTrace();// don't leave exception untreated, you always should do something with exception, write it to log, etc...
}
}
}
另请参阅本教程:What is a exception?
答案 4 :(得分:0)
throws
关键字有点像您可以购买的产品上的警告标签。它表示该方法可能抛出异常(给定类型)。此外,如果确实发生了这种类型的异常,该方法不会修复它 - 它会将异常传递给调用方法,在那里你必须处理它。
实际处理异常的标准框架(而不仅仅是将throws
放在任何地方)是try...catch
块。
try {
//Lines of code that may result in an IOException
} catch(IOException e) {
//Lines of code that handle the thrown IOException e
} finally {
//Lines of code that are run after the try...catch, regardless of what exceptions occurred.
}
如果您的try..catch块正确处理了潜在的异常,那么您将不再需要throws
关键字。
答案 5 :(得分:0)
你没有抓住你IOException
。您只使用try
块。如果捕获异常,则调用方法不需要抛出IOException
。
public static void saveObjectToTextFile(String string, String textFileName){
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File(textFileName)));
bw.write(string);
} catch(IOException e) {
} finally {
bw.close();
}
}
使用catch块捕获异常时,更具体一点是一种好习惯。这就是为什么我要抓住IOException
代替Exception
。