Use case of throws Exception in Java method! When to use throws Exception?

1. ( Ultimate explanation !!! ) Throws Exception is placed behind the method. Throws Exception means that this method does not handle exceptions and is handed over to the called party (if you do n’t want exceptions to be thrown up, you will use throws Exception) , and must be handled at the called site.
2. Throw new Exception means artificially throwing an exception, for example:
public boolean insert (News n) ​​{
try {
.....
} catch {
throw new Exception ("This is an exception thrown by myself, if I Seeing this piece of information indicates that I have made a mistake in this method. Show it to yourself! ");
} Finally {
}
}

3. First, throws Exception is added after the method to throw an exception. Among them, Exception can be understood as all exceptions, and can also throw specified exceptions. If there is no throws Exception behind the method, the method will be passed up and thrown if an exception occurs. Machine processing, the entire program will be interrupted! If you capture in the program can continue. ).

4. If there is an exception you don't need to throw up layer by layer, then you must use throws Exception , and then add try catch statement to handle when calling ... . . If there is an exception, I generally choose this method of treatment. Instead of using throws Exception, after adding throws Exception, you must add try ... catch when calling this method ( you add throw exception. You must try catch where you call, otherwise it will not compile ... The code is more robust.).

  It is equivalent to a constraint. If you don't add throws Exception, you can add try ... catch when calling the method in multiple places, but sometimes you forget to add try ... catch.

5. In addition, the principle of exception handling is to catch exceptions as early as possible, and normal programs should not write throws Exception .

6. The running exception (inheriting RuntimeException) can be uncaught and thrown upwards. If it has not been handled, the jvm will automatically handle it (stop the thread, print exception)
--- Non-runtime exceptions must be caught or declared in the method.

public class helloworld {
    public static void main (String [] args) {
        // TODO Auto-generated method stub
        // System.out.printf ("hello.java");
        try {
            test ();
        } catch (Exception e) {
            System.out.printf (e.getMessage ());
        }
    }

    / *
     *
     * throws
     * Exception: If an unknown error occurs, it will run out of Exception. If exception catch is added here, try ... catch
     * /
    private static void test () throws Exception {
        ArrayList list = new ArrayList ();
        int x = 1;
        int y = 2;
        int z = 3;
        if (x + y> = z) {
            System.out.printf ("Logic is correct!");
        } Else {
            throw new Exception ("There is a problem with the test method"); // Throw an exception manually
        }
    }
}

Guess you like

Origin www.cnblogs.com/Fooo/p/12739651.html