Java Foundation throwable, file loading

Source: Internet
Author: User

Exceptions and errors in Java are inherited from Throwable,exception and are divided into runtime exceptions (runtimeexception) and compile-time exceptions.

Run-time exception is the logic of the program is not rigorous or specific conditions under the program error, such as division when the divisor is 0, runtime exception Java is not required to Try,catch to capture. We debug the code to reduce the run-time exception, as the code debugging run-time exception is captured, the program's robustness has been improved.

Compile-time exceptions such as file not found exception, IO exception, SqlException, etc., these are predictable exceptions (checked Exception), unlike runtime exceptions that are unchecked Exception. In our code for file reads, these exceptions must be captured and handled appropriately during IO operations.

The minimum operation after an exception capture is to log logs to be documented. Throw an exception if necessary, and let the upper-level call code know that there has been an exception.

The method throws a new exception with the throw, and the method declaration throws an exception with the throws, which is also to be noted.

A subclass method throws an exception that is less or equal than the exception thrown by the parent class method, and the subclass method throws only the exception that the parent method throws or its subclass, not its parent class.

Error refers to an error in the bottom of the JVM, which is not controlled by the program, such as Stackoverflowerror,outofmemoryerror.

The common code for file loading is as follows:

InputStream is = new FileInputStream ("Src/test.properties");

This way of writing is the file once changed the path also to modify, and packaged into a running program, there is no src directory, you can also use the following method to load the file:

InputStream is = FileLoadTest.class.getClassLoader.getResourceAsStream (test.properties);

At this time as long as test.properties in Classpath below can, and specific path independent.

Java Foundation throwable, file loading

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.