0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

任意のExceptionを実行させる

Posted at

はじめに

単体テスト実施時にExceptionを発生させる必要があり、方法を調査した。

準備

catch節を適当に用意する。

    } catch (IOException ie) {
        System.out.println("IOException 例外が発生しました。");
    } catch (ArithmeticException ae) {
        System.out.println("ArithmeticException 例外が発生しました。");
    } catch (Exception e){
        System.out.println("Exception 例外が発生しました。");
    }

実行

Try節で表示タブから該当のExceptionを実行
01.png

そうすると所定のExceptionがキャッチされる
01.png

他のExceptionも同様なので必要に応じてcatch節を追加してテストを行う。

  • IOException  → throw new IOException();
  • ArithmeticException  → throw new - ArithmeticException();
  • Exception  → throw new Exception();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?