Standard handling of Java stream exceptions

Source: Internet
Author: User
Tags try catch
Package namespace; import Java. io. *; public class iotest {public static void main (string Arg []) {filewriter writer = NULL; // The reference is defined outside try, in this way, you can call try {writer = new filewriter ("F: \ java.txt") in the non-existing code block; // try writer on the code that causes the exception. write ("ABC");} catch (ioexception ex) {system. out. println (ex. tostring ();} finally // do not forget to release the resource {try // close will also cause exceptions, as well as try {If (writer! = NULL) // determine whether some resources exist during the course of disabling resources. If the resources do not exist, an exception will occur, because the first new sentence may be abnormal, in this case, the finally file contains the error writer. close ();} catch (ioexception ex) {system. out. println (ex. tostring () ;}}}/** standard write Method for Io Exception Handling * 1. define the reference outside, so that you can use it all. You can use it in a try catch code block that does not exist finally * 2. when resources are released, exceptions are also thrown, and * 3 needs to be captured. release the resource and use finally to release the resource. Determine whether the resource exists before the resource is released to check whether the resource exists. If the resource does not exist, an exception will also be thrown * 14:59:50 ****/

 

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.