Java Exceptions (1)

Source: Internet
Author: User

first, to achieve the effectIf an error occurs and some operations are not completed, the program should: (1) Return to a security state and be able to allow the user to execute some other command. (2) Allow the user to save the results of all operations and terminate the program in an appropriate manner. The task of exception handling is to control the powerfrom where the error aroseTransferred toAn error handler that can handle this situation second, abnormal classification:1, Throwable       -       ---------error       -   -  &N Bsp    -   -------xxxx       -     -       -&N Bsp    -----xxxxx       ---Exception          -    &NB sp;-                       -     -       -     -----ioexception       -     -       -     -       -     ----RunTime exception    & nbsp  -       -error error:                       Describes the errors of the Java Runtime system and the exhaustion of resources. Programs should not throw objects of this type. Runtime Exception Error: If this error proves to be your problem, it should be checked and avoided in the programming. IOException Error:        The   program itself is fine, but exceptions due to problems such as I/O errors fall into such exceptions.   Exceptions derived from RuntimeException include: (1) Error type conversion (2) array out-of-bounds access (3) an exception that is not derived from RuntimeException includes: (1) An attempt to read data after the end of the file (2) attempts to open a nonexistent file (3) attempting to String to look up a class object, and the one that represents it does not exist.  2. Checked for exceptions and not checked exceptionsThe Java language Specification will be sent in error and RuntimeException exceptions are callednot to be checked for exceptions(You will not know the error until you run it), other exceptions are calledbe checked for exceptions(Errors can be checked at compile time). Non-checked exceptions/unchecked exceptions include: (1) NULL pointer reference (2) array access out of bounds (3) Error conversion type, etc. Checked exception/checked exception (a Java Compiler for code detection) Includes: (1)IOException class and its subclasses

Third, the declaration check when the exception

Public FileInputStream (String name) throws FileNotFoundException

The compiler will then be able to tell from its header what exception this method will throw.

When should I throw an exception? Here are four things:

(1) Call a method that throws a checked exception, such as a FileInputStream constructor (controllable to handle)

(2) Errors found during program run, and throws a checked exception with throw (controllable to handle)

(3) Program error, for example a[-1]=0 will throw a arrayindexoutofboundsexception suchno exception checked (should be avoided)

(4) An internal error occurred in the Java Virtual machine and run-time library (not checked for exceptions, is an error exception ) ( not expected )
If the first two cases occur, you must tell the programmer who called this method to throw an exception.

"Editor's sentiment" Why do you design this? If the exception is responsible for (3) when checking, there will be a bunch of code that is not responsible for the program, and the exception detection is responsible for the running risk of the program, which is obviously not the original intention of the Java exception design. No (4) The situation statement is because the probability of this error is low, you can not reduce the loss after the second appearance, can only stop program execution.

Declaring multiple checked exceptions can be as follows, and remember the Comma separated:

Class Myanimation

{

Public Image Loadingimage (String s) throws FileNotFoundException, Eofexception

}

Four, throw the exception

String ReadData (Scanner in) throws eofexception

{

...

while (...)

{

if (!in.hasnext ())

{

if (n < len)

Throw new Eofexception ();

}

}

}

V. Catching 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 ();

}

}

Editing: Claruarius, reprint please indicate the source.

Java Exceptions (1)

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.