0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Java基礎学習内容7(例外)

Posted at

独自例外クラスの定義方法

独自例外
  public class MyException extends Exception {
    ...
  }

try

tryブロック内で例外が発生した場合処理を中断し、catchブロックの処理を行う。

try
  ...
  void method(){
    try{
      ...
    } catch (MyException1 | MyException2 e){
      ...
    } catch (MyException3 e){
      ...
    } finaly {
      ...
    }   
  }

# throws, throw
メソッドから呼び出し元に例外を渡す可能性がある場合throwsを用いる
また明示的に例外を投げる場合throwを用いる

```java:throws
class classA {
  void methodA() throes MyExceptionA {
    ...
    if(isError){
      throw new MyException();
    }
    ...
  }

}

※スーパークラスのメソッドでthrowsを定義していたとしても、サブクラスの同意メソッドでthrowsを記述しないことは可能・

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?