JAVA 19th (java program exception handling (2 ))

Source: Internet
Author: User

JAVA 19th (java program exception handling (2 ))

Capture of exception handling:

This is a targeted way to handle exceptions.


6. try and catch


Specific format:

Try {// code to be checked for exceptions} catch (exception variables) // The amount of changes is used to accept objects with exceptions {// code for handling exceptions} finally {// code that will be executed}

These three code blocks are a whole.

Try is an attempt, catch, finally final

When can I use try & catch or throw?

If you can solve this problem when defining a function, you can use try & catch to solve it.

Code Demonstration:

Import java. util. *; class FuException extends Exception {FuException () {} FuException (String s) {super (s); // Exception // System. out. println (s) ;}} class ExceptionDemo {// either capture or throw public int method (int [] arr, int index) throws FuException {if (index <0) {throw new FuException ("The index is fushu");} return arr [index];} public class Main {public static void main (String [] args) // throws FuException {int [] a = new int [10]; ExceptionDemo C = new ExceptionDemo (); try {int num = C. method (a,-2); System. out. print ("num" + num);} catch (FuException e) {System. out. println ("the badge is negative! "); // E the object is usable. Below are some common methods: System. out. println ("Message:" + e. getMessage (); System. out. println ("string:" + e. toString (); // toString can be omitted // print exception information e. printStackTrace (); // The default Exception Handling Mechanism of jvm is to call this method of the exception object }}}

/* Create a log file

* Log four (java ;;)

*{

*

*}

*/Its abbreviated form is log4j.


VII. Multi-catch

If multiple exceptions are thrown during program processing

Import java. util. *; class FuException extends Exception {FuException () {} FuException (String s) {super (s); // Exception // System. out. println (s) ;}} class ExceptionDemo {public int method (int [] arr, int index) throws FuException, NullPointerException {if (index <0) {throw new FuException ("The index is fushu");} if (arr = null) {throw new NullPointerException ("No array entity "); // customizable} return arr [index];} public class Main {pub Lic static void main (String [] args) // throws FuException {int [] a = new int [10]; ExceptionDemo C = new ExceptionDemo (); try {int num = C. method (a, 2); System. out. print ("num" + num);} catch (FuException e) {System. out. println ("the badge is negative! ");} Catch (NullPointerException e) {// e. printStackTrace (); System. out. println ("Message:" + e. getMessage ();} // multiple catch conditions. If the following catch conditions fail, all objects will be caught (Exception e) {// The bottom of the catch room of the parent class; otherwise, the compilation fails. // if there is a catch outside of the room, the virtual opportunity is processed }}}



VIII. Exception Handling principles


When will try be used?

The most fundamental core: As long as an exception declaration method is used, try. As long as the Code has an exception thrown action, try if it can be processed, and handle the exception once it is seen, no exception is required. Note: There are no errors. it is meaningless to write a try. There are also drawbacks. If you really want to handle exceptions but do not know what exceptions are, you can only write exceptions, once an unknown exception occurs, but there is a way to handle it in advance, you do not know what to handle, and the exception is handled. Once handled, the exception is hidden, however, the program is still running normally. Therefore, if you do not say that there is a problem, you will not handle it. If the problem occurs, you can see it and then consider whether the problem should be handled.

Exception Handling principles:

1. If the function throws an exception to be detected, the function must be declared; otherwise, try & catch must be used inside the function; otherwise, the compilation fails.

2. If you call a function that declares an exception, either try & catch or throws. Otherwise, the compilation fails.

3. When will catch and throws be implemented?

Internal functions can be solved by using catch

It cannot be solved. throws can be used by the caller.

4. If a function throws multiple exceptions, multiple catch entries must be called for targeted processing.

(If there are several internal exceptions to be detected, you need to throw a few exceptions. If you leave a few exceptions, you can catch a few exceptions)


9. finally code block


/* Finally applications:
* Most finally-used places, such as databases
* For example, open Baidu and search
* Connect to the database
* Query: If Exception occurs, a problem occurs during the query.
* Close the connection.
* If an exception occurs and the connection is not closed, database resources are still wasted. Therefore, whether or not an exception occurs, you must disable the connection.
* Release the resource, so the closed connection is defined in finally.
*/

Therefore, any resource that needs to be disabled must be defined in finally.


Try, catch, and finally code block combination:

1. try, catch, and finally (all three are common)

2. try and catch. There is no need to define finally when resources need to be released.

3. A try or finally exception cannot be directly caught, but the resource must be closed.

Void show () // throws Exception, which does not need to be declared, because catch has handled {try {throw new Exception () ;}// catchcatch {.....}}

Void show () throws Exception // if no catch occurs, you must declare {try {throw new Exception ();} // No catchfinally // to connect to the resource. An Exception occurs, however, this exception may not be handled, but the resource must be closed, because the resources opened internally do not know {....}}




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.