Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. public class Main {
  2.  
  3.   @RequiredArgsConstructor
  4.   private static class Left extends Throwable {
  5.     private final Object left;
  6.  
  7.     public <T> T getLeft() {
  8.       return (T)left;
  9.     }
  10.   }
  11.  
  12.   @RequiredArgsConstructor
  13.   private static class Right extends Throwable {
  14.     private final Object right;
  15.  
  16.     public <T> T getRight() {
  17.       return (T)right;
  18.     }
  19.   }
  20.  
  21.   private static Throwable function(int x) {
  22.     if (x > 10) {
  23.       return new Left("asdf");
  24.     }
  25.     return new Right(15);
  26.   }
  27.   public static void main(String[] args) {
  28.     var result = function(7);
  29.     try {
  30.       throw result;
  31.     } catch (Left l) {
  32.       System.out.println(l.<String>getLeft());
  33.     } catch (Right r) {
  34.       System.out.println(r.<Integer>getRight());
  35.     } catch (Throwable throwable) {
  36.       throwable.printStackTrace();
  37.     }
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement