Try and catch

Source: Internet
Author: User
First, it is clear that if there is no try, an exception will cause the program to crash.
And try to ensure the normal operation of the program, such as:
try{
int i = 1/0;
}catch (Exception e) {
........
}
A calculation, if the divisor is 0, it will be an error, if there is no try, the program directly crashes. With a try, you can let the program run, and the output why it went wrong!
Try, with the use of log4j will be very helpful for future maintenance of the program.

The code that needs to catch the exception is populated directly between the try and catch, and then an exception is caught if there is an exception that goes directly to the catch branch.
Such as:
try{
Code that needs to catch exceptions
} catch (Exception e) {
System.out.println (E.getmessage ());
}

Explanation: In the above code, if there is an error in "//code to catch the exception", the console output will be done directly.

Try and catch

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.