Files
corejava/corejava第11版/v1ch08/limitations/DefeatCheckedExceptionChecking.java
T
2019-05-22 19:43:15 +08:00

42 lines
796 B
Java

package limitations;
interface Task
{
void run() throws Exception;
@SuppressWarnings("unchecked")
static <T extends Throwable> void throwAs(Throwable t) throws T
{
throw (T) t;
}
static Runnable asRunnable(Task task)
{
return () ->
{
try
{
task.run();
}
catch (Exception e)
{
Task.<RuntimeException>throwAs(e);
}
};
}
}
public class DefeatCheckedExceptionChecking
{
public static void main(String[] args)
{
var thread = new Thread(Task.asRunnable(() ->
{
Thread.sleep(1000);
System.out.println("Hello, World!");
throw new Exception("Check this out!");
}));
thread.start();
}
}