Java exception notes and Java notes

Source: Internet
Author: User

Java exception notes and Java notes
Reference books: head First Java 1. Suppose a method is written by someone else in a class. 2. At this time, you do not know whether the method is risky (for example, if a Server failure causes the program to be affected ); 3. The best method should be to add a possible exception handling solution when calling this class. Keyword: try ...... Catch, throws, throw, finally try ...... Catch is actually a signal to the compiler, indicating that you have noticed this exception and started to handle it. The Compiler needs the programmer to try to wrap the code that may cause exceptions, then, you can use catch to handle RuntimeException, which is called a non-check exception. You can also try ...... Catch, but the compiler does not care about it. Because most runtimeexceptions are generated due to unpredictable and unavoidable problems, files may not exist all the time and the server may not crash. try ...... Catch is used to ensure that the program does not run unreasonable logic. For example, if you take the value of 8th elements for an array with only five elements, the block must be restored, at least output error messages must be friendly. Throws indicates that you are writing a program with an exception. Methods with exceptions may have to be declared through throws. After the declaration, the exception will be thrown to the code responsible for the call; at this time, the code responsible for calling needs to handle the exception. If you do not use try ...... You can also use throws to throw a catch. This behavior is called ducking in Java. However, this behavior is only used to kick the ball and the exception still exists. When it is thrown to the last layer, if the main function is also thrown, it can only be processed by JVM. The JVM and the compiler are a group, so it will certainly be kicked back to the main function, and it will still have to be processed. Therefore, an exception can be declared in the method (throws throw), not handled in the method, but handled during the call. Throw: Use throw to create the Exception object. For example, throw new Exception ("a is not equal to 11"); when creating an Exception object, add the Exception information by yourself. If throw is used, an Exception is obvious, in this case, throws must be used to throw an Exception. Of course, throws must be used to throw an Exception even if the Exception is only possible. Finally indicates that the Code finally and return of the finally package must be executed regardless of whether an exception exists. If return appears in try, the statements in finally will still be executed, it is executed before return.
Finally: The program has exited before execution (system. exit (1) in catch). Here is a function:

public static void doRisky(String test) throws Exception {    System.out.println("start risky");    if("yes".equals(test)){        throw new Exception();    }    System.out.println("end risky");        return;}

 

When executing this method, try ...... Catch:

Try {doRisky ("no");} catch (Exception e) {System. out. println ("Exception"); e. printStackTrace ();} Do you need to try ...... Catch is irrelevant to whether the input value is yes or no. The key is that doRisky has throws throwing an exception. Therefore, you must catch and process the exception during the call. The result is: if it is yes, it will be thrown. If it is no, It will be executed normally. Note the finally output here. Interestingly, if yes is input, the content in the try box will be executed first, and then finally will be output after catch. However, if there is still output after doRisky, it will not be executed, because an exception will jump directly to the catch part, that is to say, the try part after the method that generates the exception will not be executed. If no is passed in, the result will be different. try will be executed after the method about the error output: System. err. println (e. getMessage (); you can directly make error output. The output font is different. getMessage () can directly output the Exception name, and e. printStactTrace () is different. The latter outputs not only names, but also other details. Multiple exceptions. If you need to handle specific exceptions, you must use multiple catch methods, when using multiple catch methods, note that if an exception with a small scope is placed in the front, that is, the parent class exception cannot be placed in the front of the subclass exception, as if the large basket cannot be placed in a small basket.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.