我可以在Java中避免catch语句吗? [英] Can I avoid catch statement in Java ?
问题描述
当然我可以使用这样的try / catch:
String data = null;
尝试
{
扫描仪扫描=新扫描仪(新文件(文件夹+file.txt));
data = scan.nextLine();
scan.close();
}
catch(FileNotFoundException ex)
{
}
我的问题是我可以做些什么来避免try / catch?因为我不喜欢本地变量未使用。我在想这样的事情:
String data = null;
文件file_txt =新文件(文件夹+file.txt);
if(file_txt.exists())
{
扫描仪扫描=新扫描仪(file_txt);
data = scan.nextLine();
scan.close();
}
但是当然这样我在Netbeans中收到错误,我不能构建我的项目...
否,它被检查异常。 尝试必须遵循 catch 块和/或 finally 阻止。有两种处理检查异常的方法。
方法1:使用包装你的代码try / catch / finally
选项1
code> try {
扫描仪扫描=新扫描仪(新文件(文件夹+file.txt));
data = scan.nextLine();
scan.close();
}
catch(FileNotFoundException ex)
{
System.out.println(Caught+ ex);
}
选项2
尝试{
扫描仪扫描=新扫描仪(新文件夹(文件夹+file.txt));
data = scan.nextLine();
scan.close();
}
finally
{
System.out.println(Finally);
}
选项3
尝试{
扫描仪扫描=新扫描仪(新文件夹(文件夹+file.txt));
data = scan.nextLine();
scan.close();
} catch(FileNotFoundException ex){
System.out.println(Caught+ ex);
} finally {
System.out.println(Finally);
}
方法2:使用 throw抛出异常
并列出所有例外与 throws
子句。
class ThrowsDemo {
static void throwOne()throws IllegalAccessException {
System.out.println(Inside throwOne。);
抛出新的IllegalAccessException(demo);
}
public static void main(String args []){
try {
throwOne();
} catch(IllegalAccessException e){
System.out.println(Caught+ e);
}
}
}
注意: / strong>检查异常意味着编译器强制你写一些东西来处理这个错误/异常。所以,AFAIK,除了上述方法之外,没有任何替代检查的异常处理。
Inside a method, I use a Scanner to read text inside a file. This file doesn't always exist, and if it doesn't, I want simply to do nothing (i.e. no scan). Of course I could use a try/catch like this:
String data = null;
try
{
Scanner scan = new Scanner(new File(folder + "file.txt"));
data=scan.nextLine();
scan.close();
}
catch (FileNotFoundException ex)
{
}
My question is what can I do to avoid the try/catch? Because I don't like local variable unused. I was thinking of something like:
String data = null;
File file_txt = new File(folder + "file.txt");
if (file_txt.exists())
{
Scanner scan = new Scanner(file_txt);
data=scan.nextLine();
scan.close();
}
But of course with this I get an error in Netbeans and I can't build my project...
No, It's checked exception. try must be followed with either catch block and/or finally block. There are two method for handling checked exception.
Method 1 : Either wrap your code using try/catch/finally
Option 1
try{
Scanner scan = new Scanner(new File(folder + "file.txt"));
data=scan.nextLine();
scan.close();
}
catch (FileNotFoundException ex)
{
System.out.println("Caught " + ex);
}
Option 2
try{
Scanner scan = new Scanner(new File(folder + "file.txt"));
data=scan.nextLine();
scan.close();
}
finally
{
System.out.println("Finally ");
}
Option 3
try{
Scanner scan = new Scanner(new File(folder + "file.txt"));
data=scan.nextLine();
scan.close();
}catch(FileNotFoundException ex){
System.out.println("Caught " + ex );
}finally{
System.out.println("Finally ");
}
Method 2: Throw exception using throw
and list all the exception with throws
clause.
class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
Note : Checked Exception means Compiler force you to write something to handle this error/exception. So, AFAIK, there is no any alternative for checked exception handling except above method.
这篇关于我可以在Java中避免catch语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!