public class App { public static void main(String[] args) throws Throwable {} }
public class App { public static void main(String[] args) throws String {} } >> COMPILATION ERROR: Incompatible types: required 'java.lang.Throwable', found: 'java.lang.String'
public class App { public static void main(String[] args) { try { } catch (Throwable t) {} } }
public class App { public static void main(String[] args) { try { } catch (String s) {} } } >> COMPILATION ERROR: Incompatible types: required 'java.lang.Throwable', found: 'java.lang.String'
public class App { public static void main(String[] args) { // Error - Throwable throw new Error(); } }
public class App { public static void main(String[] args) { throw new String("Hello!"); } } >> COMPILATION ERROR: Incompatible types: required 'java.lang.Throwable', found: 'java.lang.String'
public class App { public static void main(String[] args) { throw null; } } >> RUNTIME ERROR: Exception in thread "main" java.lang.NullPointerException
public class App { public static void main(String[] args) { Error ref = new Error(); // throw ref; // "" } } >> RUNTIME ERROR: Exception in thread "main" java.lang.Error
public class App { public static void main(String[] args) { f(null); } public static void f(NullPointerException e) { try { throw e; } catch (NullPointerException npe) { f(npe); } } } >> RUNTIME ERROR: Exception in thread "main" java.lang.StackOverflowError
public class App { public static void main(String[] args) { System.out.println("sout"); throw new Error(); } } >> RUNTIME ERROR: Exception in thread "main" java.lang.Error >> sout
public class App { public static void main(String[] args) { System.out.println("sout"); throw new Error(); } } >> sout >> RUNTIME ERROR: Exception in thread "main" java.lang.Error
+----------------+ +->| msg2 msg1 msg0 | --> out / +----------------+ \ / +-> +--------+ | | \ +-> +--------+ \ / +------------------------> err ,
public class App { public double sqr(double arg) { // double return arg * arg; // double * double - double } }
public class App { public double sqr(double arg) { // double int k = 1; // int return k; // int double } }
// , - public class App { public double sqr(double arg) { // double int k = 1; // int return (double) k; // int double } }
public class App { public static double sqr(double arg) { return "hello!"; } } >> COMPILATION ERROR: Incompatible types. Required: double. Found: java.lang.String
public class App { public static double sqr(double arg) { } } >> COMPILATION ERROR: Missing return statement
public class App { public static double sqr(double arg) { if (System.currentTimeMillis() % 2 == 0) { return arg * arg; // currentTimeMillis() - , } // , ? } } >> COMPILATION ERROR: Missing return statement
public class App { public static void main(String[] args) { double d = sqr(10.0); // , d? System.out.println(d); } public static double sqr(double arg) { // nothing } } >> COMPILATION ERROR: Missing return statement
public class App { public static double sqr(double arg) { while (true); // , ! } }
public class App { public static void main(String[] args) { double d = sqr(10.0); // sqr - "", System.out.println(d); // d - ! } public static double sqr(double arg) { while (true); // "" } }
public class App { public static double sqr(double arg) { if (System.currentTimeMillis() % 2 == 0) { return arg * arg; // , double } else { while (true); // "" } } }
public class App { public static double sqr(double arg) { throw new RuntimeException(); } }
public class App { public static double sqr(double arg) {// double long time = System.currentTimeMillis(); if (time % 2 == 0) { return arg * arg; // , double } else if (time % 2 == 1) { { while (true); // , "" } else { throw new RuntimeException(); // } } }
public class App { public static void main(String[] args) { // sqr - "" ( "" ), double d = sqr(10.0); // main() // d - ! System.out.println(d); // ! } public static double sqr(double arg) { throw new RuntimeException(); // "" } } >> RUNTIME ERROR: Exception in thread "main" java.lang.RuntimeException
public static int area(int width, int height) {...}
public static int area(int width, int height) { return width * height; // }
public static int area(int width, int height) { if (width < 0 || height < 0) { // , } else { return width * height; } } >> COMPILATION ERROR: Missing return statement
public static int area(int width, int height) { if (width < 0 || height < 0) { System.out.println("Bad ..."); } return width * height; }
public static int area(int width, int height) { if (width < 0 || height < 0) { return -1; // "" } return width * height; }
public static int area(int width, int height) { if (width < 0 || height < 0) { System.exit(0); } return width * height; }
public static int area(int width, int height) { if (width < 0 || height < 0) { throw new IllegalArgumentException("Negative sizes: w = " + width + ", h = " + height); } return width * height; }
public class App { public static void main(String[] args) { // : int x = 42; // int y = x * x; // x = x * y; // ... } }
public class App { public static void main(String[] args) { // : if (args.length > 2) { // ... } else { // ... } // ... } }
public class App { public static void main(String[] args) { // : do..while int x = 1; do { ... } while (x++ < 10); ... } }
public class App { public static void main(String[] args) { System.err.println("#1.in"); f(); // , , System.err.println("#1.out"); // } // , public static void f() { System.err.println(". #2.in"); g(); // , , System.err.println(". #2.out"); // } // , public static void g() { System.err.println(". . #3.in"); h(); // , , System.err.println(". . #3.out"); // } // , public static void h() { System.err.println(". . . #4.in"); if (true) { System.err.println(". . . #4.RETURN"); return; // 'return' } System.err.println(". . . #4.out"); // } } >> #1.in >> . #2.in >> . . #3.in >> . . . #4.in >> . . . #4.RETURN >> . . #3.out >> . #2.out >> #1.out
public class App { public static void main(String[] args) { System.err.println("#1.in"); f(); // , , System.err.println("#1.out"); // ! } public static void f() { System.err.println(". #2.in"); g(); // , , System.err.println(". #2.out"); // ! } public static void g() { System.err.println(". . #3.in"); h(); // , , System.err.println(". . #3.out"); // ! } public static void h() { System.err.println(". . . #4.in"); if (true) { System.err.println(". . . #4.THROW"); throw new Error(); // (" ") 'throw' } System.err.println(". . . #4.out"); // ! } } >> #1.in >> . #2.in >> . . #3.in >> . . . #4.in >> . . . #4.THROW >> RUNTIME ERROR: Exception in thread "main" java.lang.Error
public class App { public static void main(String[] args) { System.err.println("#1.in"); try { f(); // , , } catch (Error e) { // "" "" System.err.println("#1.CATCH"); // } System.err.println("#1.out"); // } public static void f() { System.err.println(". #2.in"); g(); // , , System.err.println(". #2.out"); // ! } public static void g() { System.err.println(". . #3.in"); h(); // , , System.err.println(". . #3.out"); // ! } public static void h() { System.err.println(". . . #4.in"); if (true) { System.err.println(". . . #4.THROW"); throw new Error(); // (" ") 'throw' } System.err.println(". . . #4.out"); // ! } } >> #1.in >> . #2.in >> . . #3.in >> . . . #4.in >> . . . #4.THROW >> #1.CATCH >> #1.out
public class App { public static void main(String[] args) { System.err.println("#1.in"); f(); // , , System.err.println("#1.out"); // } public static void f() { System.err.println(". #2.in"); try { g(); // , , } catch (Error e) { // "" "" System.err.println(". #2.CATCH"); // } System.err.println(". #2.out"); // } public static void g() { System.err.println(". . #3.in"); h(); // , , System.err.println(". . #3.out"); // ! } public static void h() { System.err.println(". . . #4.in"); if (true) { System.err.println(". . . #4.THROW"); throw new Error(); // (" ") 'throw' } System.err.println(". . . #4.out"); // ! } } >> #1.in >> . #2.in >> . . #3.in >> . . . #4.in >> . . . #4.THROW >> . #2.CATCH >> . #2.out >> #1.out
public class App { public static void main(String[] args) { System.err.println("#1.in"); f(); // , , System.err.println("#1.out"); // } public static void f() { System.err.println(". #2.in"); g(); // , , System.err.println(". #2.out"); // } public static void g() { System.err.println(". . #3.in"); try { h(); // , , } catch (Error e) { // "" "" System.err.println(". . #3.CATCH"); // } System.err.println(". . #3.out"); // } public static void h() { System.err.println(". . . #4.in"); if (true) { System.err.println(". . . #4.THROW"); throw new Error(); // (" ") 'throw' } System.err.println(". . . #4.out"); // ! } } >> #1.in >> . #2.in >> . . #3.in >> . . . #4.in >> . . . #4.THROW >> . . #3.CATCH >> . . #3.out >> . #2.out >> #1.out
// --- RETURN--- // --- THROW--- // 1- // ( 4) #1.in #1.in . #2.in . #2.in . . #3.in . . #3.in . . . #4.in . . . #4.in . . . #4.RETURN . . . #4.THROW . . #3.out RUNTIME EXCEPTION: Exception in thread "main" java.lang.Error . #2.out #1.out // --- THROW+CATCH--- // 3- // 2- // 1- #1.in #1.in #1.in . #2.in . #2.in . #2.in . . #3.in . . #3.in . . #3.in . . . #4.in . . . #4.in . . . #4.in . . . #4.THROW . . . #4.THROW . . . #4.THROW #1.CATCH . #2.CATCH . . #3.CATCH #1.out . #2.out . . #3.out #1.out . #2.out #1.out
Object | Throwable / \ Error Exception | RuntimeException
public class App { public static void main(String[] args) { try { System.err.print(" 0"); if (true) {throw new RuntimeException();} System.err.print(" 1"); } catch (Exception e) { // catch Exception RuntimeException System.err.print(" 2"); } System.err.println(" 3"); } } >> 0 2 3
public class App { public static void main(String[] args) { try { throw new RuntimeException(); } catch (Exception e) { if (e instanceof RuntimeException) { RuntimeException re = (RuntimeException) e; System.err.print(" RuntimeException !!!"); } else { System.err.print(" RuntimeException???"); } } } } >> RuntimeException !!!
public class App { public static void main(String[] args) throws Exception { // 'throws' try { System.err.print(" 0"); if (true) {throw new Exception();} System.err.print(" 1"); } catch (RuntimeException e) { System.err.print(" 2"); } System.err.print(" 3"); } } >> 0 >> RUNTIME EXCEPTION: Exception in thread "main" java.lang.Exception
public class App { public static void main(String[] args) { try { System.err.print(" 0"); if (true) {throw new Error();} System.err.print(" 1"); } catch (Exception e) { System.err.print(" 2"); } System.err.print(" 3"); } } >> 0 >> RUNTIME EXCEPTION: Exception in thread "main" java.lang.Error
public class App { public static void main(String[] args) { try { System.err.print(" 0"); if (true) {throw new RuntimeException();} System.err.print(" 1"); } catch (RuntimeException e) { // RuntimeException System.err.print(" 2"); if (true) {throw new Error();} // Error } System.err.println(" 3"); // - Error } } >> 0 2 >> RUNTIME EXCEPTION: Exception in thread "main" java.lang.Error
public class App { public static void main(String[] args) { try { System.err.print(" 0"); if (true) {throw new RuntimeException();} System.err.print(" 1"); } catch (RuntimeException e) { // RuntimeException System.err.print(" 2"); if (true) {throw e;} // } System.err.println(" 3"); // - RuntimeException } } >> 0 2 >> RUNTIME EXCEPTION: Exception in thread "main" java.lang.RuntimeException
public class App { public static void main(String[] args) { try { System.err.print(" 0"); if (true) {throw new RuntimeException();} System.err.print(" 1"); } catch (RuntimeException e) { // RuntimeException System.err.print(" 2"); if (true) {throw new Error();} // Error } catch (Error e) { // cath Error "", System.err.print(" 3"); } System.err.println(" 4"); } } >> 0 2 >> RUNTIME EXCEPTION: Exception in thread "main" java.lang.Error
public class App { public static void main(String[] args) { try { System.err.print(" 0"); if (true) {throw new RuntimeException();} System.err.print(" 1"); } catch (RuntimeException e) { // RuntimeException System.err.print(" 2.1"); try { System.err.print(" 2.2"); if (true) {throw new Error();} // Error System.err.print(" 2.3"); } catch (Throwable t) { // Error System.err.print(" 2.4"); } System.err.print(" 2.5"); } catch (Error e) { // cath Error "", System.err.print(" 3"); } System.err.println(" 4"); } } >> 0 2.1 2.2 2.4 2.5 4
public class App { public static void main(String[] args) { try { } catch (Exception e) { } catch (RuntimeException e) { } } } >> COMPILATION ERROR: Exception 'java.lang.RuntimeException' has alredy been caught
public class App { public static void main(String[] args) { try { } catch (Error e) { } catch (RuntimeException e) { } } }
public class App { public static void main(String[] args) { try { throw new Exception(); } catch (RuntimeException e) { System.err.println("catch RuntimeException"); } catch (Exception e) { System.err.println("catch Exception"); } catch (Throwable e) { System.err.println("catch Throwable"); } System.err.println("next statement"); } } >> catch Exception >> next statement
public class App { public static void main(String[] args) { try { Throwable t = new Exception(); // Throwable Exception throw t; } catch (RuntimeException e) { System.err.println("catch RuntimeException"); } catch (Exception e) { System.err.println("catch Exception"); } catch (Throwable e) { System.err.println("catch Throwable"); } System.err.println("next statement"); } } >> catch Exception >> next statement
public class App { public static void main(String[] args) { try { System.err.println("try"); } finally { System.err.println("finally"); } } } >> try >> finally
public class App { public static void main(String[] args) { try { throw new RuntimeException(); } finally { System.err.println("finally"); } } } >> finally >> Exception in thread "main" java.lang.RuntimeException
public class App { public static void main(String[] args) { try { return; } finally { System.err.println("finally"); } } } >> finally
public class App { public static void main(String[] args) { try { System.exit(42); } finally { System.err.println("finally"); } } } >> Process finished with exit code 42
public class App { public static void main(String[] args) { try { Runtime.getRuntime().exit(42); } finally { System.err.println("finally"); } } } >> Process finished with exit code 42
public class App { public static void main(String[] args) { try { Runtime.getRuntime().halt(42); } finally { System.err.println("finally"); } } } >> Process finished with exit code 42
public class App { public static void main(String[] args) { try { System.err.println("try"); if (true) {throw new RuntimeException();} } finally { System.err.println("finally"); } System.err.println("more"); } } >> try >> finally >> Exception in thread "main" java.lang.RuntimeException
public class App { public static void main(String[] args) { try { System.err.println("try"); throw new RuntimeException(); } finally { System.err.println("finally"); } System.err.println("more"); } } >> COMPILER ERROR: Unrechable statement
public class App { public static void main(String[] args) { try { System.err.println("try"); if (true) {return;} } finally { System.err.println("finally"); } System.err.println("more"); } } >> try >> finally
public class App { public static void main(String[] args) { System.err.println(f()); } public static int f() { try { return 0; } finally { return 1; } } } >> 1
public class App { public static void main(String[] args) { System.err.println(f()); } public static int f() { try { throw new RuntimeException(); } finally { return 1; } } } >> 1
public class App { public static void main(String[] args) { System.err.println(f()); } public static int f() { try { return 0; } finally { throw new RuntimeException(); } } } >> Exception in thread "main" java.lang.RuntimeException
public class App { public static void main(String[] args) { System.err.println(f()); } public static int f() { try { throw new Error(); } finally { throw new RuntimeException(); } } } >> Exception in thread "main" java.lang.RuntimeException
// open some resource try { // use resource } finally { // close resource }
Lock lock = new ReentrantLock(); ... lock.lock(); try { // some code } finally { lock.unlock(); }
InputStream input = new FileInputStream("..."); try { // some code } finally { input.close(); }
public class App { public static void main(String[] args) { System.err.println(f()); } public static int f() { long rnd = System.currenttimeMillis(); boolean finished = false; try { if (rnd % 3 == 0) { throw new Error(); } else if (rnd % 3 == 1) { throw new RuntimeException(); } else { // nothing } finished = true; } finally { if (finished) { // } else { // , ? } } } }
public class App { public static void main(String[] args) { try { System.err.print(" 0"); // nothing System.err.print(" 1"); } catch(Error e) { System.err.print(" 2"); } finally { System.err.print(" 3"); } System.err.print(" 4"); } } >> 0 1 3 4
public class App { public static void main(String[] args) { try { System.err.print(" 0"); if (true) {throw new Error();} System.err.print(" 1"); } catch(Error e) { System.err.print(" 2"); } finally { System.err.print(" 3"); } System.err.print(" 4"); } } >> 0 2 3 4
public class App { public static void main(String[] args) { try { System.err.print(" 0"); if (true) {throw new RuntimeException();} System.err.print(" 1"); } catch(Error e) { System.err.print(" 2"); } finally { System.err.print(" 3"); } System.err.print(" 4"); } } >> 0 3 >> RUNTIME ERROR: Exception in thread "main" java.lang.RuntimeException
public class App { public static void main(String[] args) { if (args.length > 1) { if (args.length > 2) { if (args.length > 3) { ... } } } } }
public class App { public static void main(String[] args) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; i++) { for (int k = 0; k < 10; k++) { ... } } } } }
public class App { public static void main(String[] args) { try { try { try { ... } catch (Exception e) { } finally {} } catch (Exception e) { } finally {} } catch (Exception e) { } finally {} } }
public class App { public static void main(String[] args) { try { try { ... } catch (Exception e) { ... } finally { ... } } catch (Exception e) { try { ... } catch (Exception e) { ... } finally { ... } } finally { try { ... } catch (Exception e) { ... } finally { ... } } } }
public class App { public static void main(String[] args) { try { System.err.print(" 0"); try { System.err.print(" 1"); // System.err.print(" 2"); } catch (RuntimeException e) { System.err.print(" 3"); // - } finally { System.err.print(" 4"); // } System.err.print(" 5"); // - } catch (Exception e) { System.err.print(" 6"); // - } finally { System.err.print(" 7"); // } System.err.print(" 8"); // - } } >> 0 1 2 4 5 7 8
public class App { public static void main(String[] args) { try { System.err.print(" 0"); try { System.err.print(" 1"); if (true) {throw new RuntimeException();} System.err.print(" 2"); } catch (RuntimeException e) { System.err.print(" 3"); // - } finally { System.err.print(" 4"); // } System.err.print(" 5"); // - } catch (Exception e) { System.err.print(" 6"); // - , } finally { System.err.print(" 7"); // } System.err.print(" 8"); // - } } >> 0 1 3 4 5 7 8
public class App { public static void main(String[] args) { try { System.err.print(" 0"); try { System.err.print(" 1"); if (true) {throw new Exception();} System.err.print(" 2"); } catch (RuntimeException e) { System.err.print(" 3"); // - , } finally { System.err.print(" 4"); // } System.err.print(" 5"); // - } catch (Exception e) { System.err.print(" 6"); // - } finally { System.err.print(" 7"); // } System.err.print(" 8"); // - } } >> 0 1 4 6 7 8
public class App { public static void main(String[] args) { try { System.err.print(" 0"); try { System.err.print(" 1"); if (true) {throw new Error();} System.err.print(" 2"); } catch (RuntimeException e) { System.err.print(" 3"); // - , } finally { System.err.print(" 4"); // } System.err.print(" 5"); // - } catch (Exception e) { System.err.print(" 6"); // - , } finally { System.err.print(" 7"); // } System.err.print(" 8"); // - } } >> 0 1 4 7 >> RUNTIME EXCEPTION: Exception in thread "main" java.lang.Error
Source: https://habr.com/ru/post/223821/
All Articles