需要捕获IOExceptions&例外 [英] The need to catch IOExceptions & Exceptions

查看:123
本文介绍了需要捕获IOExceptions&例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用扫描器对象在txt文件中读取的程序只产生FileNotFoundException,那么应该总是捕获IOException / Exception?



这些额外的代码是不需要的还是重要的?

  import java .io.File; 
import java.io.FileNotFoundException;
import java.util.Scanner;


public class TestingScanner {
public TestingScanner(){
readFile(dummy.txt);
}

public void readFile(String filePath){
文件文件=新建文件(filePath);
扫描仪扫描仪= null;

try {
scanner = new Scanner(file);

while(scanner.hasNext()){
String line = scanner.nextLine();
if(line.matches(^。+ @ yahoo\\\.com?(\\\.uk)?$))
System.out.println(line);
}
} catch(FileNotFoundException e){
System.out.println(错误:无法加载到+ filePath中);
e.printStackTrace();
} finally {
if(scanner!= null)
scanner.close();
}
}
}


解决方案

我的经验法则是广泛地捕捉异常(捕获异常或Throwable),除非有特定的东西,我会为特定的异常做不同的。



例如,如果一个方法抛出两个不同的异常,我将处理这两个异常,然后我将捕获异常。但是,如果我正在处理它们不同,那么我会单独捕获每个并且相应地处理它们。



这种方法中的扳手是运行时异常。一个想法是让RuntimeExceptions冒泡起来。但是我发现,在我遇到异常的情况下,我想要所有的...,有时甚至是Throwables(只有通过捕捉异常而不是Throwable才被烧掉)。



这里有一些例子:

  public void myMethod()throws IOException,FileNotFoundException(); 

在我不想让异常冒泡的情况下(需要处理它们) p>

  try {
myMethod();
catch(异常e){
//处理
}

在我遇到异常的情况下,需要为FileNotFound做不同的事情。

  try {
myMethod );
catch(FileNotFoundException fe){
//处理文件未找到
}
catch(异常e){
//处理
}

在我正在让异常起泡的情况下,因为我进一步了解了链条,一些非常酷的异常处理代码是处理它,我想避免多次记录异常:

  myMethod(); 

在我正在让异常起泡的情况下,除了FileNotFound。

  try {
myMethod();
catch(FileNotFoundException fe){
//未找到句柄文件
}


Should one always catch an IOException/Exception if a program reading in a txt file using a scanner object produces nothing but a FileNotFoundException?

Would such extra code be unneeded or important?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class TestingScanner {
    public TestingScanner() {
        readFile("dummy.txt");
    }

    public void readFile(String filePath) {
        File file = new File(filePath);
        Scanner scanner = null;

        try {
            scanner = new Scanner(file);

            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                if (line.matches("^.+@yahoo\\.com?(\\.uk)?$"))
                    System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            System.out.println("ERROR: Couldn't Load in " + filePath);
            e.printStackTrace();
        } finally {
            if (scanner != null)
                scanner.close();
        }
    }
}

解决方案

My rule of thumb is to go broad with exception catching (catching Exception or Throwable) unless there is something specific I will want do different for a specific exception.

For example, if a method throws two different Exceptions, and I will be handling both the same then I will just catch Exception. But if I am handling them different then I would catch each individually and process them accordingly.

The wrench in this approach is "what about RuntimeExceptions". One school of thought is to allow RuntimeExceptions to bubble up. But what I find is that in situations where I am catching exceptions, then I want all of them... and sometimes even Throwables (I got burnt once by only catching Exception and not Throwable).

Here are some examples:

public void myMethod() throws IOException, FileNotFoundException ();

In situation where I want to not let Exceptions bubble up (need to deal with them all)

try{
   myMethod();
catch(Exception e) {
//handle it
}

In situation where I am catching Exceptions and need to do something different for FileNotFound.

try{
   myMethod();
catch(FileNotFoundException  fe){
    //handle file not found
}
catch(Exception e) {
    //handle it
}

In situation where I am letting Exceptions bubble up because I know further up the chain some really cool exception handling code is handling it and I want to avoid logging the exception multiple times:

myMethod();

In situation where I am letting Exceptions bubble up except for FileNotFound.

try{
   myMethod();
catch(FileNotFoundException  fe){
    //handle file not found
}

这篇关于需要捕获IOExceptions&例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆