Java Base--try-catch-finally Statement

Source: Internet
Author: User
Tags try catch

Q: Does the Java exception handling mechanism understand? Java exception handling, really mastered it? What is a custom exception? What do you do with return in the catch body? What do you do with return in the finally body? What happens when the catch and the finally body meet return? Some examples to clarify them.

One, try Catch statement

Format: (Code area returns to exception handling if there is an error)

1 Try {23// code area 45 } Catch (Exception e) {67// exception handling 89 }

First, be aware that exceptions have run-time exceptions and check exceptions:

Run exceptions do not require forced capture, they are subclasses of RuntimeException. Check exceptions require a try, catch exception handling.

The API can be called to query, such as the common Runtimexception have indexoutofboundsexception (index over bounds), nullpointerexception (null pointer exception) and so on. Common check exceptions are IO exceptions, SQL exceptions, FileNotFoundException (date conversion exceptions), and so on.

In severe words, if a class with a high frequency access does not have a try, an exception can cause the program to crash. And try to ensure the normal operation of the program, such as:

1 Try {2int i = 1/0; 3 }catch(Exception e) {4... 5 }

As small as a calculation, if the divisor is 0, it will be an error, if there is no try, the program directly hung off. With a try, you can let the program run, and the output why it went wrong!

Try catch is to catch the exception of the try part, when you do not have a try catch, if there is an exception, the program error, plus try catch, the exception program normal operation, just the error information stored in the exception, so catch is used to extract the exception information, You can add an System.out.println (E.tostring ()) to the Catch section, and you can print the exception if an exception occurs.

code example:

1  Public classTestEx1 {2      Public intAddintAintb) {3         returnA +b;4     }5 6      Public intDevintAintb) {7         intc = A/b;//There's a danger here .8         returnC;9     }Ten  One      Public Static voidMain (string[] args) { ATestEx1 C =NewTestEx1 (); -         intresult = C.add (4, 9); -SYSTEM.OUT.PRINTLN ("The result of summing is" +result); the         Try{//Prevention -             intResult_chu = C.dev (90, 0); -SYSTEM.OUT.PRINTLN ("Except for the result is" +Result_chu); -}Catch(Exception e) { +System.out.println ("Wrong ~"); -  +         } A     } at}

Java Base--try-catch-finally Statement

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.