Java exception (1), Java (1)

Source: Internet
Author: User

Java exception (1), Java (1)
1. if an error occurs but some operations are not completed, the program should: (1) return to a security status and allow the user to execute some other commands. (2) allow the user to save the results of all operations and terminate the program in an appropriate way. The task of exception handling is to controlFrom where errors are generatedTransferError processor capable of handling this situation Ii. Exception classification:1. Throwable---------- Error---------- XXXX-------- XXXXX --- Exception------- IOException------- RunTime Exception--Error: describes system errors and resource depletion errors during Java runtime. The program should not throw this type of object. Runtime Exception error: If this error occurs, it turns out to be a problem and should be checked and avoided in programming. IOException error: the program itself is normal, but exceptions caused by problems such as I/O errors belong to this type of exception. Exceptions derived from RuntimeException include: (1) Error type conversion (2) array out-of-bounds access (3) exceptions where the access control pointer is not derived from RuntimeException include: (1) try to read data after the end of the file (2) try to open a non-existent file (3) Try to find the Class Object Based on the given string, and the Class represented by this string does not exist.2. Checked exceptions and unchecked exceptionsThe Java language specification refers to exceptions derived from Error and RuntimeExceptionExceptions not checked(The error is known only after running). Other exceptions are calledChecked exception(Errors can be checked during compilation ). Not checked exceptions/not checked exceptions include: (1) NULL pointer reference (2) array access out of bounds (3) Incorrect conversion type, etc. Checked exceptions/checked exceptions (Java compiler checks the code.) Includes: (1)IOException class and its subclass

 

Iii. Exceptions during declared checks

Public FileInputStream (String name)Throws FileNotFoundException

This wayCompilerIt will know from its header what exceptions this method will throw.

When should I throw an exception? The following four situations:

(1) call a method that throws checked exceptions, such as the FileInputStream Constructor (controllable and manageable)

(2) errors found during the running of the program, and throws a checked exception using throw (controllable and manageable)

(3) An error occurs in the program. For example, if a [-1] = 0, an ArrayIndexOutOfBoundsException such Unchecked exceptions (should be avoided)

 

(4) internal errors occur in Java virtual machines and runtime libraries (Error)(Unexpected)
In the first two cases, the programmer who calls this method must be informed that an exception may be thrown.

[Editor's sentiment]Why is it designed like this? If an exception is the responsibility of (3), a pile of codes that are not responsible for the program will appear, and the exception detection is responsible for the program running risks, this is clearly not the original intention of Java exception design. Incorrect (4) situation declaration is caused by a low probability of such errors. After the second occurrence, you will not be able to reduce the loss caused by it. You can only stop program execution.

Declare multiple checked exceptions as follows. Separate them with commas:

Class MyAnimation

{

Public Image loadingImage (String s)Throws FileNotFoundException, EOFException

}

4. Throw an exception

String readData (bytes in)Throws EOFException

{

...

While (...)

{

If (! In. hasNext ())

{

If (n <len)

ThrowNew EOFException ();

}

}

}

5. Capture exceptions

Public void read (String file)

{

Try {

InputStream in = new FileInputStream (file );

Int B;

While (B = in. read ())! =-1)

[

// PROCESS INPUT

}

} Catch (IOException exception ){

Exception. printStackTrace ();

}

}

Edit: Claruarius. For more information, see the source.

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.