fork download
  1. import java.io.*;
  2.  
  3. class A{
  4. public static void main(String[] args){
  5. try{
  6. f();
  7. if(false){throw(IOException)null;}
  8. }
  9. catch(IOException e){e.printStackTrace();}
  10. }
  11. static void f(){
  12. g();
  13. }
  14. static void g(){
  15. Thrower.throw2(new IOException("io"));
  16. }
  17. }
  18.  
  19. class Thrower{
  20. private static ThreadLocal<Exception> tl = new ThreadLocal<Exception>();
  21. private Thrower()throws Exception{
  22. Exception e = tl.get();
  23. tl.remove();
  24. throw e;
  25. }
  26. public static void throw2(Exception ex){
  27. if(ex==null){throw new IllegalArgumentException("ex==null");}
  28. if(ex instanceof RuntimeException){throw (RuntimeException)ex;}
  29. try{
  30. tl.set(ex);
  31. Thrower.class.newInstance();
  32. }
  33. catch(IllegalAccessException e){throw new RuntimeException(e);}
  34. catch(InstantiationException e){throw new RuntimeException(e);}
  35. }
  36. }
  37.  
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
Standard output is empty