Javaカスタム例外の例

Javaには、チェック例外と未チェック例外の2種類の例外があります。 概要は次のとおりです。
-
Checked –
java.lang.Exceptionを拡張します。回復可能な状態の場合は、例外を明示的にキャッチして、エラーをコンパイルしてください。 -
Unchecked –
java.lang.RuntimeExceptionを拡張し、プログラミングエラー、try-catchの必要なし、ランタイムエラーなどの回復不能な状態に対応します。
1. カスタムチェック例外
Note
いくつかの一般的なチェック例外:IOException、FileNotFoundException
1.1 If the client is able to recover from the exception, make it a checked exception. カスタムチェック例外を作成するには、java.lang.Exceptionを拡張します
NameNotFoundException.java
package com.example.examples.exception;
public class NameNotFoundException extends Exception {
public NameNotFoundException(String message) {
super(message);
}
}
1.2 For checked exception, you need to try and catch the exception.
CustomerService.java
package com.example.examples;
import com.example.examples.exception.NameNotFoundException;
public class CustomerService {
public Customer findByName(String name) throws NameNotFoundException {
if ("".equals(name)) {
throw new NameNotFoundException("Name is empty!");
}
return new Customer(name);
}
public static void main(String[] args) {
CustomerService obj = new CustomerService();
try {
Customer cus = obj.findByName("");
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
出力
com.example.examples.exception.NameNotFoundException: Name is empty!
at com.example.examples.CustomerService.findByName(CustomerService.java:10)
at com.example.examples.CustomerService.main(CustomerService.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
2. カスタム未チェック例外
Note
いくつかの一般的なチェックされていない例外:NullPointerException、IndexOutOfBoundsException、IllegalArgumentException
2.1 If the client cannot do anything to recover from the exception, make it an unchecked exception. 未チェックのカスタム例外を作成するには、java.lang.RuntimeExceptionを拡張します
ListTooLargeException.java
package com.example.examples.exception;
public class ListTooLargeException extends RuntimeException{
public ListTooLargeException(String message) {
super(message);
}
}
2.3 For unchecked exception, try and catch the exception is optional.
CustomerService.java
package com.example.examples;
import com.example.examples.exception.ListTooLargeException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomerService {
public void analyze(List data) {
if (data.size() > 50) {
//runtime exception
throw new ListTooLargeException("List can't exceed 50 items!");
}
//...
}
public static void main(String[] args) {
CustomerService obj = new CustomerService();
//create 100 size
List data = new ArrayList<>(Collections.nCopies(100, "example"));
obj.analyze(data);
}
}
出力
Exception in thread "main" com.example.examples.exception.ListTooLargeException: List can't exceed 50 items!
at com.example.examples.CustomerService.analyze(CustomerService.java:25)
at com.example.examples.CustomerService.main(CustomerService.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)