{"id":455541,"date":"2025-04-11T09:09:31","date_gmt":"2025-04-11T09:09:31","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=455541"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=455541","title":{"rendered":"<span>\u041a\u0430\u043a \u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 throws \u0432 Java: \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0440\u0430\u0437\u0431\u043e\u0440 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u00a0Java \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0442\u0430\u0440\u043e\u0435, \u043d\u043e\u00a0\u0432\u0441\u0451 \u0435\u0449\u0451 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u0431\u0440\u043e\u0441\u0430\u0435\u0442 <strong>checked\u2011\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/strong> (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, IOException), \u043e\u043d \u043e\u0431\u044f\u0437\u0430\u043d \u044f\u0432\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u044d\u0442\u043e \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e throws. \u042d\u0442\u043e \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435\u00a0\u2014 \u043d\u0435\u00a0\u043e\u0442\u00a0JVM, \u0430\u00a0\u043e\u0442\u00a0\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0436\u0451\u0441\u0442\u043a\u043e \u0441\u043b\u0435\u0434\u0438\u0442 \u0437\u0430\u00a0\u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0446\u0438\u044f\u043c\u0438, \u043d\u043e\u00a0\u0432\u043e\u0442 JVM\u00a0\u2014 \u043d\u0435\u0442. \u0427\u0442\u043e\u00a0\u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u0443\u044e \u043b\u0430\u0437\u0435\u0439\u043a\u0443: <strong>\u043e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440, \u043e\u0441\u0442\u0430\u0432\u0430\u044f\u0441\u044c \u043f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c \u0432\u00a0\u0440\u0430\u043c\u043a\u0430\u0445 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 JVM<\/strong>.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u043a\u0430\u043a\u00a0\u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>Unsafe.throwException()<\/code>, \u043f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0433\u0434\u0435 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u044c\u0441\u044f, \u0438 \u043d\u0430\u00a0\u0447\u0442\u043e\u00a0\u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Java 17, 21\u00a0\u0438 24.<\/p>\n<blockquote>\n<p>Unsafe\u00a0\u2014 \u044d\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 API. \u041e\u043d \u043d\u0435\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439, \u043c\u043e\u0436\u0435\u0442\u00a0\u0431\u044b\u0442\u044c \u0443\u0434\u0430\u043b\u0451\u043d \u0438\u043b\u0438\u00a0\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d \u0432\u00a0\u0431\u0443\u0434\u0443\u0449\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 JDK. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u00a0\u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435\u043c \u0438 \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<\/blockquote>\n<h3>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: checked-\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u0431\u0435\u0437 throws<\/h3>\n<p>\u0412\u00a0Java \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432, \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u00a0\u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 throws: Runnable, Callable, Supplier, Consumer, Function \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0435. \u042d\u0442\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f\u00a0\u043b\u044f\u043c\u0431\u0434, \u043d\u043e\u00a0\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0435\u0441\u043b\u0438 \u0432\u0434\u0440\u0443\u0433 \u0432\u00a0\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u043e \u0431\u0440\u043e\u0441\u0438\u0442\u044c IOException, SQLException \u0438\u043b\u0438\u00a0\u043b\u044e\u0431\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435 checked\u2011\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"java\">Runnable r = () -&gt; {     throw new IOException(\"\u041d\u0435 \u043c\u043e\u0433\u0443 \u0432 \u0441\u0435\u0442\u044c\"); \/\/ \u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 };<\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0433\u043e\u0432\u043e\u0440\u0438\u0442: \u043d\u0435\u043b\u044c\u0437\u044f. JVM\u00a0\u0431\u044b \u0441\u00a0\u0440\u0430\u0434\u043e\u0441\u0442\u044c\u044e \u0438\u0441\u043f\u043e\u043b\u043d\u0438\u043b\u0430\u00a0\u2014 \u043d\u043e\u00a0\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435\u00a0\u0434\u0430\u0441\u0442 \u0441\u043e\u0431\u0440\u0430\u0442\u044c.<\/p>\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u0435: Unsafe.throwException(Throwable)<\/h3>\n<p>\u0421\u0442\u0430\u0440\u044b\u0439 \u0434\u043e\u0431\u0440\u044b\u0439 <code>sun.misc.Unsafe<\/code>, \u043e\u0434\u0438\u043d \u0438\u0437\u00a0\u0441\u0430\u043c\u044b\u0445 \u043c\u043e\u0449\u043d\u044b\u0445 (\u0438 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0430\u0441\u043d\u044b\u0445) \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0432\u00a0Java. \u0412\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434:<\/p>\n<pre><code class=\"java\">public native void throwException(Throwable t);<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043a\u0438\u0434\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <strong>\u0432\u00a0\u043e\u0431\u0445\u043e\u0434 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430<\/strong>. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0JVM \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442 \u044d\u0442\u043e. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u00a0\u2014 \u043d\u0435\u0442. \u0410\u00a0Unsafe \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u00a0\u0443\u0440\u043e\u0432\u043d\u0435 JVM.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"java\">import sun.misc.Unsafe; import java.lang.reflect.Field; import java.io.IOException;  public class UnsafeThrower {     private static final Unsafe unsafe = getUnsafe();      public static void sneakyThrow(Throwable t) {         unsafe.throwException(t);     }      private static Unsafe getUnsafe() {         try {             Field f = Unsafe.class.getDeclaredField(\"theUnsafe\");             f.setAccessible(true);             return (Unsafe) f.get(null);         } catch (Exception e) {             throw new RuntimeException(\"\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c Unsafe\", e);         }     }      public static void main(String[] args) {         sneakyThrow(new IOException(\"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430\"));         System.out.println(\"\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f\");     } }<\/code><\/pre>\n<p>\u0421\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f. \u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432\u044b\u043b\u0435\u0442\u0438\u0442. throws \u043d\u0438\u0433\u0434\u0435 \u043d\u0435\u00a0\u043d\u0430\u043f\u0438\u0441\u0430\u043d\u00a0\u2014 \u043d\u043e\u00a0\u0432\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442? \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Java \u0441\u0442\u0440\u043e\u0433\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u0442 checked \u0438 unchecked \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. \u041d\u043e <strong>JVM \u043e\u0431\u00a0\u044d\u0442\u043e\u043c \u043d\u0435\u00a0\u0437\u043d\u0430\u0435\u0442 \u0432\u043e\u0432\u0441\u0435<\/strong>. \u0412\u00a0\u0431\u0430\u0439\u0442\u043a\u043e\u0434\u0435 \u043d\u0435\u0442 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 <code>RuntimeException<\/code> \u0438 <code>IOException<\/code>. \u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e athrow, \u0438 JVM \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 Throwable \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e\u00a0\u0441\u0442\u0435\u043a\u0443 \u0432\u044b\u0437\u043e\u0432\u043e\u0432.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>Unsafe.throwException()<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 athrow \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, <strong>\u043e\u0431\u0445\u043e\u0434\u044f \u0432\u0441\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0441\u043a\u0438\u0435 \u0431\u0430\u0440\u044c\u0435\u0440\u044b<\/strong>. \u042d\u0442\u043e \u0434\u0430\u0451\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u0442\u0435\u043c, \u0447\u0442\u043e\u00a0\u0432\u044b\u043b\u0435\u0442\u0430\u0435\u0442 \u0438\u0437\u00a0\u043c\u0435\u0442\u043e\u0434\u0430.<\/p>\n<h3>\u0411\u0430\u0439\u0442\u043a\u043e\u0434 \u0433\u043b\u0430\u0437\u0430\u043c\u0438 JVM<\/h3>\n<pre><code class=\"bash\">javap -c -p UnsafeThrower<\/code><\/pre>\n<p>\u0412\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u0432\u0440\u043e\u0434\u0435:<\/p>\n<pre><code>0: new           #2                  \/\/ class java\/io\/IOException 3: dup 4: ldc           #3                  \/\/ String \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 6: invokespecial #4                  \/\/ Method java\/io\/IOException.&lt;init&gt; 9: invokestatic  #5                  \/\/ Method sneakyThrow:(Ljava\/lang\/Throwable;)V 12: return<\/code><\/pre>\n<p>\u0412\u0441\u0451, \u0447\u0442\u043e\u00a0\u043d\u0443\u0436\u043d\u043e\u00a0\u2014 \u043f\u0440\u044f\u043c\u043e\u0439 \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430, \u0431\u0440\u043e\u0441\u0430\u044e\u0449\u0435\u0433\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. JVM \u043d\u0435\u00a0\u0441\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u00a0throws.<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f<\/h3>\n<h4>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043e\u0448\u0438\u0431\u043e\u043a<\/h4>\n<p>\u041c\u043e\u043a\u0430\u0435\u0442\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u00a0\u043c\u043e\u0436\u0435\u0442 \u0431\u0440\u043e\u0441\u0430\u0442\u044c IOException, \u043d\u043e\u00a0\u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439:<\/p>\n<pre><code class=\"java\">when(service.fetch()).thenAnswer(inv -&gt; {     UnsafeThrower.sneakyThrow(new IOException(\"\u0421\u0435\u0440\u0432\u0438\u0441 \u0443\u043f\u0430\u043b\"));     return null; });<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u043a\u0430\u043a\u00a0\u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u00a0\u0441\u0435\u0442\u0435\u0432\u044b\u043c\u0438 \u043e\u0448\u0438\u0431\u043a\u0430\u043c\u0438.<\/p>\n<h4>DI-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b, Proxy, Reflection<\/h4>\n<pre><code class=\"java\">try {     Method m = clazz.getMethod(\"run\");     m.invoke(obj); } catch (InvocationTargetException e) {     UnsafeThrower.sneakyThrow(e.getCause()); }<\/code><\/pre>\n<p>\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0447\u0442\u043e\u00a0\u0443\u0433\u043e\u0434\u043d\u043e. \u0411\u0435\u0437\u00a0throws. \u041d\u043e\u00a0\u043d\u0435\u00a0\u0442\u0435\u0440\u044f\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u00a0\u2014 \u043f\u0440\u043e\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043a\u00a0\u0435\u0441\u0442\u044c.<\/p>\n<h4>\u041b\u044f\u043c\u0431\u0434\u044b \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b<\/h4>\n<pre><code class=\"java\">@FunctionalInterface interface ThrowingRunnable {     void run() throws Exception; }  static Runnable wrap(ThrowingRunnable r) {     return () -&gt; {         try {             r.run();         } catch (Exception e) {             UnsafeThrower.sneakyThrow(e);         }     }; }<\/code><\/pre>\n<p>\u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u043b\u043e\u0436\u0438\u0442\u0441\u044f \u0432\u00a0<code>Executors.newSingleThreadExecutor().submit(...)<\/code>.<\/p>\n<h3>\u0421\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u044b<\/h3>\n<h4>Generic cast hack \u2014 type erasure<\/h4>\n<pre><code class=\"bash\">@SuppressWarnings(\"unchecked\") public static &lt;T extends Throwable&gt; void sneakyThrow(Throwable t) throws T {     throw (T) t; }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 \u043e\u0431\u043c\u0430\u043d\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e generic type erasure. \u0422\u044b \u043e\u0431\u0435\u0449\u0430\u0435\u0448\u044c, \u0447\u0442\u043e\u00a0\u0431\u0440\u043e\u0441\u0430\u0435\u0448\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 T, \u043d\u043e\u00a0\u043d\u0430\u00a0\u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043a\u0438\u0434\u0430\u0435\u0448\u044c \u0432\u0441\u0451, \u0447\u0442\u043e\u00a0\u0443\u0433\u043e\u0434\u043d\u043e. JVM \u043d\u0435\u00a0\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0442\u0438\u043f T, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043e\u043d \u0441\u0442\u0435\u0440\u0442 \u0432\u00a0\u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435\u00a0\u0440\u0443\u0433\u0430\u0435\u0442\u0441\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u0434\u0443\u043c\u0430\u0435\u0442, \u0447\u0442\u043e\u00a0\u0442\u044b \u043a\u0438\u0434\u0430\u0435\u0448\u044c Throwable, \u0430\u00a0\u043d\u0435, \u0441\u043a\u0430\u0436\u0435\u043c, IOException.<\/p>\n<p>\u042d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0447\u0443\u0442\u044c \u043c\u0435\u043d\u0435\u0435 \u0440\u0438\u0441\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0439, \u0447\u0435\u043c Unsafe, \u043d\u043e\u00a0\u0442\u0440\u0435\u0431\u0443\u0435\u0442 suppress warnings \u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f generics.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"css\">public static &lt;T&gt; Supplier&lt;T&gt; sneakySupplier(ThrowingSupplier&lt;T&gt; supplier) {     return () -&gt; {         try {             return supplier.get();         } catch (Exception e) {             return sneakyThrow(e);         }     }; }<\/code><\/pre>\n<h4>Lombok @SneakyThrows \u2014 compile-time<\/h4>\n<pre><code class=\"bash\">@SneakyThrows void method() {     throw new IOException(\"\u041b\u043e\u043c\u0431\u043e\u043a! \u041b\u043e\u043c\u0431\u043e\u043a \u043f\u043e\u0432\u0441\u044e\u0434\u0443\"); }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438\u043d\u0430\u0447\u0435. Lombok \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 AST \u0432\u043e\u00a0\u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0438 <strong>\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 throws Throwable \u0432\u00a0\u0431\u0430\u0439\u0442\u043a\u043e\u0434<\/strong>, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0442\u044b \u0435\u0433\u043e \u043d\u0435\u00a0\u043f\u0438\u0448\u0435\u0448\u044c \u0432\u00a0\u043a\u043e\u0434\u0435. \u0412\u00a0\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u0442 throws, \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u044b \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u00a0\u0432\u0438\u0434\u0438\u0448\u044c.<\/p>\n<p>\u041d\u043e\u00a0\u0435\u0441\u0442\u044c \u043c\u0438\u043d\u0443\u0441\u044b. \u0412\u0441\u0435 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u00a0compile\u2011time \u0438 \u043d\u0435\u00a0\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u043b\u044f\u00a0runtime\u2011\u043f\u0440\u043e\u043a\u0438\u0434\u043a\u0438 (\u0447\u0435\u0440\u0435\u0437 reflection, DI \u0438\u00a0\u0442.\u00a0\u043f.)<\/p>\n<h4>\u0415\u0441\u043b\u0438 \u0442\u044b \u043d\u0430 Java 17, 21, 24:<\/h4>\n<pre><code class=\"bash\">@SuppressWarnings(\"unchecked\") public static &lt;T extends Throwable&gt; void sneakyThrow(Throwable t) throws T {     throw (T) t; }<\/code><\/pre>\n<p>\u0414\u043b\u044f\u00a0jdk.internal.misc.Unsafe:<\/p>\n<pre><code class=\"bash\">--add-exports java.base\/jdk.internal.misc=ALL-UNNAMED<\/code><\/pre>\n<p>\u0411\u0435\u0437\u00a0\u044d\u0442\u043e\u0433\u043e \u043d\u0435\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0434\u0430\u0436\u0435 \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u044e \u0434\u043e\u0441\u0442\u0430\u0442\u044c Unsafe.<\/p>\n<hr\/>\n<p>\u0410\u00a0\u0432\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441\u00a0\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u0433\u0434\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c throws? \u0414\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0432\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u043d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u0438\u0451\u043c\u044b, \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Java-\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u043e\u0439 \u2014 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0439\u0442\u0435\u0441\u044c \u043a \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u0443\u0440\u043e\u043a\u0430\u043c \u0432 Otus, \u0433\u0434\u0435 \u043c\u044b \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043a\u0435\u0439\u0441\u044b \u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<ul>\n<li>\n<p><strong>8 \u0430\u043f\u0440\u0435\u043b\u044f<\/strong>. \u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0442\u0435\u0440\u044f\u044e\u0442\u0441\u044f: \u0411\u0440\u043e\u043a\u0435\u0440\u044b \u043f\u0440\u043e\u0442\u0438\u0432 \u0445\u0430\u043e\u0441\u0430 \u0432 Java.<br \/><a href=\"https:\/\/otus.pw\/7FI7U\/\">\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><strong>22 \u0430\u043f\u0440\u0435\u043b\u044f<\/strong>. JDBC \u2014 \u0432\u0430\u0448 \u0448\u0432\u0435\u0439\u0446\u0430\u0440\u0441\u043a\u0438\u0439 \u043d\u043e\u0436 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<br \/><a href=\"https:\/\/otus.pw\/5NE4\/\">\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/897384\/\"> https:\/\/habr.com\/ru\/articles\/897384\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u00a0Java \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0442\u0430\u0440\u043e\u0435, \u043d\u043e\u00a0\u0432\u0441\u0451 \u0435\u0449\u0451 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u0431\u0440\u043e\u0441\u0430\u0435\u0442 <strong>checked\u2011\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/strong> (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, IOException), \u043e\u043d \u043e\u0431\u044f\u0437\u0430\u043d \u044f\u0432\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u044d\u0442\u043e \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e throws. \u042d\u0442\u043e \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435\u00a0\u2014 \u043d\u0435\u00a0\u043e\u0442\u00a0JVM, \u0430\u00a0\u043e\u0442\u00a0\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0436\u0451\u0441\u0442\u043a\u043e \u0441\u043b\u0435\u0434\u0438\u0442 \u0437\u0430\u00a0\u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0446\u0438\u044f\u043c\u0438, \u043d\u043e\u00a0\u0432\u043e\u0442 JVM\u00a0\u2014 \u043d\u0435\u0442. \u0427\u0442\u043e\u00a0\u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u0443\u044e \u043b\u0430\u0437\u0435\u0439\u043a\u0443: <strong>\u043e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440, \u043e\u0441\u0442\u0430\u0432\u0430\u044f\u0441\u044c \u043f\u0440\u0438\u00a0\u044d\u0442\u043e\u043c \u0432\u00a0\u0440\u0430\u043c\u043a\u0430\u0445 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 JVM<\/strong>.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u043a\u0430\u043a\u00a0\u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>Unsafe.throwException()<\/code>, \u043f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0433\u0434\u0435 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u044c\u0441\u044f, \u0438 \u043d\u0430\u00a0\u0447\u0442\u043e\u00a0\u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Java 17, 21\u00a0\u0438 24.<\/p>\n<blockquote>\n<p>Unsafe\u00a0\u2014 \u044d\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 API. \u041e\u043d \u043d\u0435\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439, \u043c\u043e\u0436\u0435\u0442\u00a0\u0431\u044b\u0442\u044c \u0443\u0434\u0430\u043b\u0451\u043d \u0438\u043b\u0438\u00a0\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d \u0432\u00a0\u0431\u0443\u0434\u0443\u0449\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 JDK. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u00a0\u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435\u043c \u0438 \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<\/blockquote>\n<h3>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: checked-\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u0431\u0435\u0437 throws<\/h3>\n<p>\u0412\u00a0Java \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432, \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u00a0\u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 throws: Runnable, Callable, Supplier, Consumer, Function \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0435. \u042d\u0442\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f\u00a0\u043b\u044f\u043c\u0431\u0434, \u043d\u043e\u00a0\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0435\u0441\u043b\u0438 \u0432\u0434\u0440\u0443\u0433 \u0432\u00a0\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u043e \u0431\u0440\u043e\u0441\u0438\u0442\u044c IOException, SQLException \u0438\u043b\u0438\u00a0\u043b\u044e\u0431\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435 checked\u2011\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"java\">Runnable r = () -&gt; {     throw new IOException(\"\u041d\u0435 \u043c\u043e\u0433\u0443 \u0432 \u0441\u0435\u0442\u044c\"); \/\/ \u041e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 };<\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0433\u043e\u0432\u043e\u0440\u0438\u0442: \u043d\u0435\u043b\u044c\u0437\u044f. JVM\u00a0\u0431\u044b \u0441\u00a0\u0440\u0430\u0434\u043e\u0441\u0442\u044c\u044e \u0438\u0441\u043f\u043e\u043b\u043d\u0438\u043b\u0430\u00a0\u2014 \u043d\u043e\u00a0\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435\u00a0\u0434\u0430\u0441\u0442 \u0441\u043e\u0431\u0440\u0430\u0442\u044c.<\/p>\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u0435: Unsafe.throwException(Throwable)<\/h3>\n<p>\u0421\u0442\u0430\u0440\u044b\u0439 \u0434\u043e\u0431\u0440\u044b\u0439 <code>sun.misc.Unsafe<\/code>, \u043e\u0434\u0438\u043d \u0438\u0437\u00a0\u0441\u0430\u043c\u044b\u0445 \u043c\u043e\u0449\u043d\u044b\u0445 (\u0438 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0430\u0441\u043d\u044b\u0445) \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0432\u00a0Java. \u0412\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434:<\/p>\n<pre><code class=\"java\">public native void throwException(Throwable t);<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043a\u0438\u0434\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 <strong>\u0432\u00a0\u043e\u0431\u0445\u043e\u0434 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430<\/strong>. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0JVM \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442 \u044d\u0442\u043e. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u00a0\u2014 \u043d\u0435\u0442. \u0410\u00a0Unsafe \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u00a0\u0443\u0440\u043e\u0432\u043d\u0435 JVM.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"java\">import sun.misc.Unsafe; import java.lang.reflect.Field; import java.io.IOException;  public class UnsafeThrower {     private static final Unsafe unsafe = getUnsafe();      public static void sneakyThrow(Throwable t) {         unsafe.throwException(t);     }      private static Unsafe getUnsafe() {         try {             Field f = Unsafe.class.getDeclaredField(\"theUnsafe\");             f.setAccessible(true);             return (Unsafe) f.get(null);         } catch (Exception e) {             throw new RuntimeException(\"\u041d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c Unsafe\", e);         }     }      public static void main(String[] args) {         sneakyThrow(new IOException(\"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430\"));         System.out.println(\"\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f\");     } }<\/code><\/pre>\n<p>\u0421\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f. \u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432\u044b\u043b\u0435\u0442\u0438\u0442. throws \u043d\u0438\u0433\u0434\u0435 \u043d\u0435\u00a0\u043d\u0430\u043f\u0438\u0441\u0430\u043d\u00a0\u2014 \u043d\u043e\u00a0\u0432\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442? \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Java \u0441\u0442\u0440\u043e\u0433\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u0442 checked \u0438 unchecked \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. \u041d\u043e <strong>JVM \u043e\u0431\u00a0\u044d\u0442\u043e\u043c \u043d\u0435\u00a0\u0437\u043d\u0430\u0435\u0442 \u0432\u043e\u0432\u0441\u0435<\/strong>. \u0412\u00a0\u0431\u0430\u0439\u0442\u043a\u043e\u0434\u0435 \u043d\u0435\u0442 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 <code>RuntimeException<\/code> \u0438 <code>IOException<\/code>. \u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e athrow, \u0438 JVM \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 Throwable \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e\u00a0\u0441\u0442\u0435\u043a\u0443 \u0432\u044b\u0437\u043e\u0432\u043e\u0432.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 <code>Unsafe.throwException()<\/code> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 athrow \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, <strong>\u043e\u0431\u0445\u043e\u0434\u044f \u0432\u0441\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0441\u043a\u0438\u0435 \u0431\u0430\u0440\u044c\u0435\u0440\u044b<\/strong>. \u042d\u0442\u043e \u0434\u0430\u0451\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u0442\u0435\u043c, \u0447\u0442\u043e\u00a0\u0432\u044b\u043b\u0435\u0442\u0430\u0435\u0442 \u0438\u0437\u00a0\u043c\u0435\u0442\u043e\u0434\u0430.<\/p>\n<h3>\u0411\u0430\u0439\u0442\u043a\u043e\u0434 \u0433\u043b\u0430\u0437\u0430\u043c\u0438 JVM<\/h3>\n<pre><code class=\"bash\">javap -c -p UnsafeThrower<\/code><\/pre>\n<p>\u0412\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u0432\u0440\u043e\u0434\u0435:<\/p>\n<pre><code>0: new           #2                  \/\/ class java\/io\/IOException 3: dup 4: ldc           #3                  \/\/ String \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 6: invokespecial #4                  \/\/ Method java\/io\/IOException.&lt;init&gt; 9: invokestatic  #5                  \/\/ Method sneakyThrow:(Ljava\/lang\/Throwable;)V 12: return<\/code><\/pre>\n<p>\u0412\u0441\u0451, \u0447\u0442\u043e\u00a0\u043d\u0443\u0436\u043d\u043e\u00a0\u2014 \u043f\u0440\u044f\u043c\u043e\u0439 \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430, \u0431\u0440\u043e\u0441\u0430\u044e\u0449\u0435\u0433\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. JVM \u043d\u0435\u00a0\u0441\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u00a0throws.<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f<\/h3>\n<h4>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043e\u0448\u0438\u0431\u043e\u043a<\/h4>\n<p>\u041c\u043e\u043a\u0430\u0435\u0442\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u00a0\u043c\u043e\u0436\u0435\u0442 \u0431\u0440\u043e\u0441\u0430\u0442\u044c IOException, \u043d\u043e\u00a0\u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439:<\/p>\n<pre><code class=\"java\">when(service.fetch()).thenAnswer(inv -&gt; {     UnsafeThrower.sneakyThrow(new IOException(\"\u0421\u0435\u0440\u0432\u0438\u0441 \u0443\u043f\u0430\u043b\"));     return null; });<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u043a\u0430\u043a\u00a0\u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u00a0\u0441\u0435\u0442\u0435\u0432\u044b\u043c\u0438 \u043e\u0448\u0438\u0431\u043a\u0430\u043c\u0438.<\/p>\n<h4>DI-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b, Proxy, Reflection<\/h4>\n<pre><code class=\"java\">try {     Method m = clazz.getMethod(\"run\");     m.invoke(obj); } catch (InvocationTargetException e) {     UnsafeThrower.sneakyThrow(e.getCause()); }<\/code><\/pre>\n<p>\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0447\u0442\u043e\u00a0\u0443\u0433\u043e\u0434\u043d\u043e. \u0411\u0435\u0437\u00a0throws. \u041d\u043e\u00a0\u043d\u0435\u00a0\u0442\u0435\u0440\u044f\u0435\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u00a0\u2014 \u043f\u0440\u043e\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043a\u00a0\u0435\u0441\u0442\u044c.<\/p>\n<h4>\u041b\u044f\u043c\u0431\u0434\u044b \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b<\/h4>\n<pre><code class=\"java\">@FunctionalInterface interface ThrowingRunnable {     void run() throws Exception; }  static Runnable wrap(ThrowingRunnable r) {     return () -&gt; {         try {             r.run();         } catch (Exception e) {             UnsafeThrower.sneakyThrow(e);         }     }; }<\/code><\/pre>\n<p>\u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u043b\u043e\u0436\u0438\u0442\u0441\u044f \u0432\u00a0<code>Executors.newSingleThreadExecutor().submit(...)<\/code>.<\/p>\n<h3>\u0421\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u044b<\/h3>\n<h4>Generic cast hack \u2014 type erasure<\/h4>\n<pre><code class=\"bash\">@SuppressWarnings(\"unchecked\") public static &lt;T extends Throwable&gt; void sneakyThrow(Throwable t) throws T {     throw (T) t; }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 \u043e\u0431\u043c\u0430\u043d\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e generic type erasure. \u0422\u044b \u043e\u0431\u0435\u0449\u0430\u0435\u0448\u044c, \u0447\u0442\u043e\u00a0\u0431\u0440\u043e\u0441\u0430\u0435\u0448\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 T, \u043d\u043e\u00a0\u043d\u0430\u00a0\u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043a\u0438\u0434\u0430\u0435\u0448\u044c \u0432\u0441\u0451, \u0447\u0442\u043e\u00a0\u0443\u0433\u043e\u0434\u043d\u043e. JVM \u043d\u0435\u00a0\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0442\u0438\u043f T, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u043e\u043d \u0441\u0442\u0435\u0440\u0442 \u0432\u00a0\u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435\u00a0\u0440\u0443\u0433\u0430\u0435\u0442\u0441\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e\u00a0\u0434\u0443\u043c\u0430\u0435\u0442, \u0447\u0442\u043e\u00a0\u0442\u044b \u043a\u0438\u0434\u0430\u0435\u0448\u044c Throwable, \u0430\u00a0\u043d\u0435, \u0441\u043a\u0430\u0436\u0435\u043c, IOException.<\/p>\n<p>\u042d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0447\u0443\u0442\u044c \u043c\u0435\u043d\u0435\u0435 \u0440\u0438\u0441\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0439, \u0447\u0435\u043c Unsafe, \u043d\u043e\u00a0\u0442\u0440\u0435\u0431\u0443\u0435\u0442 suppress warnings \u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f generics.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"css\">public static &lt;T&gt; Supplier&lt;T&gt; sneakySupplier(ThrowingSupplier&lt;T&gt; supplier) {     return () -&gt; {         try {             return supplier.get();         } catch (Exception e) {             return sneakyThrow(e);         }     }; }<\/code><\/pre>\n<h4>Lombok @SneakyThrows \u2014 compile-time<\/h4>\n<pre><code class=\"bash\">@SneakyThrows void method() {     throw new IOException(\"\u041b\u043e\u043c\u0431\u043e\u043a! \u041b\u043e\u043c\u0431\u043e\u043a \u043f\u043e\u0432\u0441\u044e\u0434\u0443\"); }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438\u043d\u0430\u0447\u0435. Lombok \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 AST \u0432\u043e\u00a0\u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0438 <strong>\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 throws Throwable \u0432\u00a0\u0431\u0430\u0439\u0442\u043a\u043e\u0434<\/strong>, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0442\u044b \u0435\u0433\u043e \u043d\u0435\u00a0\u043f\u0438\u0448\u0435\u0448\u044c \u0432\u00a0\u043a\u043e\u0434\u0435. \u0412\u00a0\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u0442 throws, \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u044b \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u00a0\u0432\u0438\u0434\u0438\u0448\u044c.<\/p>\n<p>\u041d\u043e\u00a0\u0435\u0441\u0442\u044c \u043c\u0438\u043d\u0443\u0441\u044b. \u0412\u0441\u0435 \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u00a0compile\u2011time \u0438 \u043d\u0435\u00a0\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u043b\u044f\u00a0runtime\u2011\u043f\u0440\u043e\u043a\u0438\u0434\u043a\u0438 (\u0447\u0435\u0440\u0435\u0437 reflection, DI \u0438\u00a0\u0442.\u00a0\u043f.)<\/p>\n<h4>\u0415\u0441\u043b\u0438 \u0442\u044b \u043d\u0430 Java 17, 21, 24:<\/h4>\n<pre><code class=\"bash\">@SuppressWarnings(\"unchecked\") public static &lt;T extends Throwable&gt; void sneakyThrow(Throwable t) throws T {     throw (T) t; }<\/code><\/pre>\n<p>\u0414\u043b\u044f\u00a0jdk.internal.misc.Unsafe:<\/p>\n<pre><code class=\"bash\">--add-exports java.base\/jdk.internal.misc=ALL-UNNAMED<\/code><\/pre>\n<p>\u0411\u0435\u0437\u00a0\u044d\u0442\u043e\u0433\u043e \u043d\u0435\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0434\u0430\u0436\u0435 \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u044e \u0434\u043e\u0441\u0442\u0430\u0442\u044c Unsafe.<\/p>\n<hr\/>\n<p>\u0410\u00a0\u0432\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441\u00a0\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u0433\u0434\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c throws? \u0414\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0432\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u043d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u0438\u0451\u043c\u044b, \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Java-\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u043e\u0439 \u2014 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0439\u0442\u0435\u0441\u044c \u043a \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u0443\u0440\u043e\u043a\u0430\u043c \u0432 Otus, \u0433\u0434\u0435 \u043c\u044b \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043a\u0435\u0439\u0441\u044b \u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<ul>\n<li>\n<p><strong>8 \u0430\u043f\u0440\u0435\u043b\u044f<\/strong>. \u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0442\u0435\u0440\u044f\u044e\u0442\u0441\u044f: \u0411\u0440\u043e\u043a\u0435\u0440\u044b \u043f\u0440\u043e\u0442\u0438\u0432 \u0445\u0430\u043e\u0441\u0430 \u0432 Java.<br \/><a href=\"https:\/\/otus.pw\/7FI7U\/\">\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><strong>22 \u0430\u043f\u0440\u0435\u043b\u044f<\/strong>. JDBC \u2014 \u0432\u0430\u0448 \u0448\u0432\u0435\u0439\u0446\u0430\u0440\u0441\u043a\u0438\u0439 \u043d\u043e\u0436 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<br \/><a href=\"https:\/\/otus.pw\/5NE4\/\">\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/897384\/\"> https:\/\/habr.com\/ru\/articles\/897384\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-455541","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/455541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=455541"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/455541\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=455541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=455541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=455541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}