Created
October 24, 2020 13:02
-
-
Save RayStarkMC/404f1cf8fcf0a1e5c3060af8141a4953 to your computer and use it in GitHub Desktop.
Try型による静的例外処理の例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import raystark.eflib.exhandler.Try2; | |
public class SampleTry2 { | |
//チェック例外型 | |
static class LargerThan10Exception extends Exception {} | |
static class LargerThan20Exception extends Exception {} | |
static class MiddleException extends Exception {} | |
//可変データ | |
private static int value; | |
//例外を投げるメソッド | |
private static int throwingMethod() throws LargerThan10Exception, LargerThan20Exception { | |
if(value > 20) throw new LargerThan20Exception(); | |
if(value > 10) throw new LargerThan10Exception(); | |
return value; | |
} | |
private static int handle20(LargerThan20Exception x) { | |
System.out.println("Handling LargerThan20Exception."); | |
return 1; | |
} | |
private static int handle10(LargerThan10Exception x) { | |
System.out.println("Handling LargerThan10Exception."); | |
return 0; | |
} | |
private static void handleFinally() { | |
System.out.println("Handling finally block."); | |
} | |
private static void Handle20throwing() throws MiddleException{ | |
throw new MiddleException(); | |
} | |
private static void finally1() { | |
System.out.println("Finally1"); | |
} | |
private static void finally2() { | |
System.out.println("Finally2"); | |
} | |
public static void main(String[] args) { | |
//Try型のオブジェクト作成 | |
//例外を投げる可能性のあるSupplier | |
var try2 = Try2.builder(LargerThan10Exception.class, LargerThan20Exception.class) | |
.build(SampleTry2::throwingMethod); | |
value = 100; | |
//rawGetの場合宣言した例外の処理が必須 | |
try { | |
try2.rawGet(); | |
} catch (LargerThan10Exception e) { | |
System.out.println("LargerThan10Exception!"); | |
} catch (LargerThan20Exception e) { | |
System.out.println("LargerThen20Exception!"); //こちらが出力される | |
} | |
//各例外に対するハンドラを設定 | |
var supplier1 = try2.recover2( | |
SampleTry2::handle10, | |
SampleTry2::handle20, | |
SampleTry2::handleFinally | |
); | |
value = 100; | |
System.out.println(supplier1.get()); | |
// Handling LargerThan20Exception, | |
// Handling finally block. | |
// 1 | |
value = 15; | |
System.out.println(supplier1.get()); | |
// Handling LargerThan10Exception. | |
// Handling finally block. | |
// 0 | |
value = 10; | |
System.out.println(supplier1.get()); | |
// Handling finally block. | |
//10 | |
//ハンドラは部分的に設定することも可能 | |
//この場合最初に設定したハンドラに対応する例外が発生しなくてもfinally1は実行される | |
//try文がネストする事になる | |
//これをネストしないようにするかは検討中 | |
var supplier2 = try2.recover1(SampleTry2::handle10, SampleTry2::finally1) | |
.recover1(SampleTry2::handle20, SampleTry2::finally2); | |
value = 100; | |
System.out.println(supplier2.get()); | |
// Finally1 | |
// Handling LargerThan20Exception. | |
// Finally2 | |
// 1 | |
value = 15; | |
System.out.println(supplier2.get()); | |
// Handling LargerThan10Exception. | |
// Finally1 | |
// Finally2 | |
// 0 | |
value = 10; | |
System.out.println(supplier2.get()); | |
//Finally1 | |
//Finally2 | |
//10 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment