Object | Throwable / \ Error Exception | RuntimeException
Object | Throwable(CHECKED) / \ Error(UNCHECKED) Exception(CHECKED) | RuntimeException(UNCHECKED)
public class App { public static void main(String[] args) { throw new Exception(); // } } >> COMPILATION ERROR: unhandled exception: java.lang.Exception
import java.io.IOException; public class App { public static void main(String[] args) throws IOException { throw new Exception(); // } } >> COMPILATION ERROR: unhandled exception: java.lang.Exception
public class App { public static void main(String[] args) throws Exception { // Exception throw new Exception(); // Exception } }
public class App { public static void main(String[] args) throws Throwable { // "" Throwable throw new Exception(); // Exception } }
public class App { public static void main(String[] args) throws Exception { // // } }
public class App { public static void main(String[] args) { f(); // } public static void f() throws Exception { } } >> COMPILATION ERROR: unhandled exception: java.lang.Exception
public class App { // Throwable public static void main(String[] args) throws Throwable { f(); } // - Exception public static void f() throws Exception { } }
public class InternetDownloader { public static byte[] (String url) throws IOException { return "<html><body>Nothing! It's stub!</body></html>".getBytes(); } }
public class App { public static void main(String[] args) { f(); } public static void f() throws RuntimeException { } }
package java.lang; public final class Integer extends Number implements Comparable<Integer> { ... /** * ... * * @param sa {@code String} containing the {@code int} * representation to be parsed * @return the integer value represented by the argument in decimal. * @exception NumberFormatException if the string does not contain a * parsable integer. */ public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); } ... }
import java.io.EOFException; import java.io.FileNotFoundException; public class App { // public static void main(String[] args) throws EOFException, FileNotFoundException { if (System.currentTimeMillis() % 2 == 0) { throw new EOFException(); } else { throw new FileNotFoundException(); } } }
import java.io.EOFException; import java.io.FileNotFoundException; public class App { // public static void main(String[] args) throws EOFException, FileNotFoundException { f0(); f1(); } public static void f0() throws EOFException {...} public static void f1() throws FileNotFoundException {...} }
import java.io.EOFException; import java.io.FileNotFoundException; import java.io.IOException; public class App { // public static void main(String[] args) throws IOException { if (System.currentTimeMillis() % 2 == 0) { throw new EOFException(); } else { throw new FileNotFoundException(); } } }
import java.io.EOFException; import java.io.FileNotFoundException; public class App { // public static void main(String[] args) throws IOException { f0(); f1(); } public static void f0() throws EOFException {...} public static void f1() throws FileNotFoundException {...} }
import java.io.EOFException; import java.io.FileNotFoundException; public class App { public static void main(String[] args) throws IOException, InterruptedException { f0(); f1(); f2(); } public static void f0() throws EOFException {...} public static void f1() throws FileNotFoundException {...} public static void f2() throws InterruptedException {...} }
public class App { public static void main(String[] args) { try { throw new Exception(); } catch (Exception e) { // ... } } }
public class App { public static void main(String[] args) { try { throw new Exception(); } catch (Throwable e) { // ... } } }
public class App { public static void main(String[] args) { try { throw new Throwable(); } catch (Exception e) { // ... } } } >> COMPILATION ERROR: unhandled exception: java.lang.Throwable
public class App { public static void main(String[] args) { try { throw new Exception(); } catch (Error e) { // ... } } } >> COMPILATION ERROR: unhandled exception: java.lang.Exception
import java.io.EOFException; import java.io.FileNotFoundException; public class App { // EOFException catch-, public static void main(String[] args) throws FileNotFoundException { try { if (System.currentTimeMillis() % 2 == 0) { throw new EOFException(); } else { throw new FileNotFoundException(); } } catch (EOFException e) { // ... } } }
public class App { // Exception public static void main(String[] args) throws Exception { Throwable t = new Exception(); // Exception throw t; // } } >> COMPILATION ERROR: unhandled exception: java.lang.Throwable
public class App { public static void main(String[] args) throws Exception { Object ref = "Hello!"; // ref char c = ref.charAt(0); // } } >> COMPILATION ERROR: Cannot resolve method 'charAt(int)'
// ! ! public class App { public static void f0(Throwable t) throws Exception { throw t; } public static void f1(Object ref){ char c = ref.charAt(0); } }
public class App { // Exception public static void main(String[] args) throws Exception { try { Throwable t = new Exception(); // Exception throw t; // } catch (Exception e) { System.out.println("!"); } } } >> COMPILATION ERROR: unhandled exception: java.lang.Throwable
public class App { // Throwable public static void main(String[] args) throws Throwable { try { Throwable t = new Exception(); // Exception throw t; } catch (Exception e) { // Exception System.out.println("!"); } } } >> !
import java.io.FileNotFoundException; import java.io.IOException; public class Parent { // IOException InterruptedException public void f() throws IOException, InterruptedException {} } class Child extends Parent { // IOException @Override public void f() throws FileNotFoundException {} }
import java.io.IOException; public class Parent { public void f() throws IOException, InterruptedException {} } class ChildB extends Parent { @Override public void f() throws Exception {} } >> COMPILATION ERROR: overridden method does not throw 'java.lang.Exception'
public class Parent { // Exception public void f() throws Exception {} }
// Java, , , public class Child extends Parent { // Exception Throwable public void f() throws Throwable {} }
public class Demo { public static void test(Parent ref) { // , Parent.f() Exception catch try { ref.f(); } catch(Exception e) {} } }
public class App { public static void main(String[] args) { // , Demo.test Parent - Child Demo.test(new Child()); } }
Object | Throwable(CHECKED) / \ Error(UNCHECKED) Exception(CHECKED) | RuntimeException(UNCHECKED)
Object | Throwable(CHECKED) / | \ Error(UNCHECKED) | Exception(CHECKED) | | | | | A(UNC) D(UNC) | F(C) RuntimeException(UNCHECKED) / \ | / \ | | B(UNC) C(UNC) | G(C) H(C) I(UNC) J(UNC) E(C) / \ K(UNC) L(UNC)
Source: https://habr.com/ru/post/225585/
All Articles