質問

JavaでReadersとStreamsを使用することに関して私を常に悩ませることの1つは、 close()メソッドが例外をスローできることです。 closeメソッドをfinallyブロックに配置することをお勧めします。そのため、少し厄介な状況が必要になります。私は通常この構造を使用します:

FileReader fr = new FileReader("SomeFile.txt");
try {
    try {
        fr.read();
    } finally {
        fr.close();
    }
} catch(Exception e) {
    // Do exception handling
}

しかし、この構造も見ました:

FileReader fr = new FileReader("SomeFile.txt");
try {
    fr.read() 
} catch (Exception e) {
    // Do exception handling
} finally {
    try {
        fr.close();
    } catch (Exception e) {
        // Do exception handling
    }
}

キャッチブロックは1つしかなく、よりエレガントに見えるため、私は最初の構成を好みます。実際に2番目または代替の構造を好む理由はありますか?

更新: read close の両方がIOExceptionsのみをスローすることを指摘した場合、違いが生じますか?したがって、読み取りが失敗した場合、同じ理由でクローズが失敗する可能性があります。

役に立ちましたか?

解決

常に最初の例に進みます。

closeが例外をスローする場合(実際にはFileReaderでは発生しません)、呼び出し側に適した例外をスローする標準的な処理方法ではないでしょうか?近い例外は、ほぼ間違いなく、リソースを使用していた問題に勝ります。例外処理のアイデアがSystem.err.printlnを呼び出すことである場合、2番目の方法がおそらくより適切です。

例外をどこまでスローするかという問題があります。 ThreadDeathは常に再スローする必要がありますが、最終的に例外が発生すると停止します。同様に、ErrorはRuntimeExceptionよりもさらにスローされ、RuntimeExceptionはチェックされた例外よりもさらにスローされます。本当にしたい場合は、これらの規則に従うコードを記述し、「実行」で抽象化します。イディオム。

他のヒント

最初の例には大きな問題があるのではないかと思います。つまり、読み取り中または読み取り後に例外が発生すると、 finally ブロックが実行されます。ここまでは順調ですね。しかし、 fr.close()が原因で別の例外がスローされた場合はどうなりますか?これは「切り札」となります。最初の例外( return finally ブロックに入れるようなビット)および問題の実際の原因に関するすべての情報を失う

最終的にブロックを使用する必要があります:

IOUtil.closeSilently(fr);

このユーティリティメソッドが行うこと:

public static void closeSilently(Closeable c) {
    try { c.close(); } catch (Exception e) {} 
} 

2番目の方が好きです。どうして? read() close()の両方が例外をスローした場合、そのうちの1つが失われる可能性があります。最初の構成では、 close()からの例外が read()からの例外をオーバーライドし、2番目の構成では close()は個別に処理されます。


Java 7以降、 try-with-リソースの構造により、これがはるかに簡単になります。例外を気にせずに読むには:

try (FileReader fr = new FileReader("SomeFile.txt")) {
    fr.read();
    // no need to close since the try-with-resources statement closes it automatically
}

例外処理あり:

try (FileReader fr = new FileReader("SomeFile.txt")) {
    fr.read();
    // no need to close since the try-with-resources statement closes it automatically
} catch (IOException e) {
    // Do exception handling
    log(e);
    // If this catch block is run, the FileReader has already been closed.
    // The exception could have come from either read() or close();
    // if both threw exceptions (or if multiple resources were used and had to be closed)
    // then only one exception is thrown and the others are suppressed
    // but can still be retrieved:
    Throwable[] suppressed = e.getSuppressed(); // can be an empty array
    for (Throwable t : suppressed) {
        log(suppressed[t]);
    }
}

必要なtry-catchは1つだけで、すべての例外を安全に処理できます。必要に応じて finally ブロックを追加できますが、リソースを閉じる必要はありません。

read close の両方が例外をスローした場合、 read からの例外はオプション1で非表示になります。したがって、2番目のオプションはより多くのエラー処理。

ただし、ほとんどの場合、最初のオプションが優先されます。

  1. 多くの場合、生成されたメソッドで例外を処理することはできませんが、その操作内でストリーム処理をカプセル化する必要があります。
  2. コードにライターを追加して、2番目のアプローチがどの程度冗長になるかを確認してください。

生成されたすべての例外を渡す必要がある場合は、それを行うことができます

違いは、私が見る限り、異なるレベルでのプレイには異なる例外と原因があり、

catch(例外e)

はそれを覆い隠します。複数のレベルの唯一のポイントは、あなたの例外とあなたがそれらについてどうするかを区別することです:

try
{
  try{
   ...
  }
   catch(IOException e)
  {
  ..
  }
}
catch(Exception e)
{
  // we could read, but now something else is broken 
  ...
}

通常は次のことを行います。最初に、try / catch messを処理するテンプレートメソッドベースのクラスを定義します

import java.io.Closeable;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public abstract class AutoFileCloser {
    private static final Closeable NEW_FILE = new Closeable() {
        public void close() throws IOException {
            // do nothing
        }
    };

    // the core action code that the implementer wants to run
    protected abstract void doWork() throws Throwable;

    // track a list of closeable thingies to close when finished
    private List<Closeable> closeables_ = new LinkedList<Closeable>();

    // mark a new file
    protected void newFile() {
        closeables_.add(0, NEW_FILE);
    }

    // give the implementer a way to track things to close
    // assumes this is called in order for nested closeables,
    // inner-most to outer-most
    protected void watch(Closeable closeable) {
        closeables_.add(0, closeable);
    }

    public AutoFileCloser() {
        // a variable to track a "meaningful" exception, in case
        // a close() throws an exception
        Throwable pending = null;

        try {
            doWork(); // do the real work

        } catch (Throwable throwable) {
            pending = throwable;

        } finally {
            // close the watched streams
            boolean skip = false;
            for (Closeable closeable : closeables_) {
                if (closeable == NEW_FILE) {
                    skip = false;
                } else  if (!skip && closeable != null) {
                    try {
                        closeable.close();
                        // don't try to re-close nested closeables
                        skip = true;
                    } catch (Throwable throwable) {
                        if (pending == null) {
                            pending = throwable;
                        }
                    }
                }
            }

            // if we had a pending exception, rethrow it
            // this is necessary b/c the close can throw an
            // exception, which would remove the pending
            // status of any exception thrown in the try block
            if (pending != null) {
                if (pending instanceof RuntimeException) {
                    throw (RuntimeException) pending;
                } else {
                    throw new RuntimeException(pending);
                }
            }
        }
    }
}

「保留中」に注意してください。例外-これは、クローズ中にスローされた例外が、本当に気になる例外をマスクするケースを処理します。

最後に、装飾されたストリームの外側から最初に閉じようとします。したがって、FileWriterをラップするBufferedWriterがある場合、最初にBuffereredWriterを閉じようとしますが、失敗した場合は、FileWriter自体を閉じようとします。

次のように上記のクラスを使用できます:

try {
    // ...

    new AutoFileCloser() {
        @Override protected void doWork() throws Throwable {
            // declare variables for the readers and "watch" them
            FileReader fileReader = null;
            BufferedReader bufferedReader = null;
            watch(fileReader = new FileReader("somefile"));
            watch(bufferedReader = new BufferedReader(fileReader));

            // ... do something with bufferedReader

            // if you need more than one reader or writer
            newFile(); // puts a flag in the 
            FileWriter fileWriter = null;
            BufferedWriter bufferedWriter = null;
            watch(fileWriter = new FileWriter("someOtherFile"));
            watch(bufferedWriter = new BufferedWriter(fileWriter));

            // ... do something with bufferedWriter
        }
    };

    // .. other logic, maybe more AutoFileClosers

} catch (RuntimeException e) {
    // report or log the exception
}

このアプローチを使用すると、ファイルのクローズを再度処理するためにtry / catch / finallyを心配する必要はありません。

これが重すぎて使用できない場合は、少なくともtry / catchと&quot; pending&quot;に従うことを検討してください。使用する変数アプローチ。

私が使用する標準的な規則では、finallyブロックから例外をエスケープさせてはいけません。

これは、例外がすでに伝播している場合、finallyブロックからスローされた例外が元の例外に勝る(したがって失われる)ためです。

元の例外が問題の原因である可能性があるため、99%のケースではこれは望みのものではありません(二次的な例外は最初の副作用である場合がありますが、したがって、実際の問題)。

したがって、基本的なコードは次のようになります。

try
{
    // Code
}
// Exception handling
finally
{
    // Exception handling that is garanteed not to throw.
    try
    {
         // Exception handling that may throw.
    }
    // Optional Exception handling that should not throw
    finally()
    {}
}

2番目のアプローチ。

それ以外の場合、FileReaderコンストラクターから例外をキャッチすることはありません

http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileReader.html#FileReader(java.lang.String)

public FileReader(String fileName)            FileNotFoundExceptionをスローします

そのため、通常、tryブロック内にもコンストラクターがあります。 finallyブロックは、クローズを試みる前にリーダーがnullでないかどうかを確認します。

Datasource、Connection、Statement、ResultSetにも同じパターンが適用されます。

ネストされたtry-catchが優先されない場合があります。これを考慮してください:

try{
 string s = File.Open("myfile").ReadToEnd(); // my file has a bunch of numbers
 // I want to get a total of the numbers 
 int total = 0;
 foreach(string line in s.split("\r\n")){
   try{ 
     total += int.Parse(line); 
   } catch{}
 }
catch{}

これはおそらく悪い例ですが、ネストされたtry-cactchが必要になる場合があります。

@Chris Marshallによるアプローチは気に入っていますが、例外が静かに飲み込まれるのを見るのは好きではありません。例外を記録するのが最善だと思います。特にあなたが関係なく続けているなら。

私は常にユーティリティクラスを使用してこの種の一般的な例外を処理しますが、これを彼の答えとは少し違うものにします。

エラーなどをログに記録するには、常にロガー(私にとってはlog4j)を使用します。

IOUtil.close(fr);

ユーティリティメソッドのわずかな変更:

public static void close(Closeable c) {
    try {
      c.close();
    } catch (Exception e) {
      logger.error("An error occurred while closing. Continuing regardless", e); 
    } 
}

場合によっては、ネストされたTry-Catchが避けられないことがあります。たとえば、エラー回復コード自体が例外をスローできる場合。ただし、コードの可読性を向上させるために、ネストされたブロックを独自のメソッドに常に抽出できます。ネストされたTry-最後にブロックをキャッチします。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top