Quantcast
Channel: 例外タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 100

[Java] コンパイルエラーと実行時エラーの分類メモ

$
0
0

コンパイルエラーになるケースと実行時エラーになるケースの分類。

コンパイルエラーになるケース

ラムダ式の型の不一致

BiFunction<Integer, Double, Integer> function = (x, y) -> x + y;
function.apply(1, 2.5);

(int)(x + y) もしくは、 BiFunction<Integer, Double, Double> に修正する必要がある。

実行時になるケース

FileInputStreamでreset()を呼び出し

FileInputStreamはreset()を呼び出せるがサポートはしていない。子クラスのBufferedInputStreamでサポートされる。

new FileInputStream("src/a/a.txt").reset();
//=> java.io.IOException: mark/reset not supported

親クラスのInputStreamのreset()

public synchronized void reset() throws IOException {
        throw new IOException("mark/reset not supported");
    }

子クラスのBufferedInputStreamのreset()

public synchronized void reset() throws IOException {
        getBufIfOpen(); // Cause exception if closed
        if (markpos < 0)
            throw new IOException("Resetting to invalid mark");
        pos = markpos;
    }

Viewing all articles
Browse latest Browse all 100

Trending Articles