Java public class -04-exception

Source: Internet
Author: User
Tags throwable

I. Concept of exceptions:

When the program is running, we do not anticipate the situation, it prevents the program according to our expected effect execution!

Two. All exceptions and errors of the parent class--(Throwable)

The Throwable class is the top-level parent of the Java exception type, and an object is only an instance of the () direct or indirect of the Throwable class, which is an exception object that can be identified by the exception handling mechanism. Some common exception classes are built into the JDK, and we can also customize exceptions.

1.excetion--exception

(1). Run-time exception (Runtimeexction): An exception that is generated during a program run.

01:arithmeticexception--Mathematical Anomalies

02:inputmismatchexception--input Type mismatch exception

03:nullpointexception--NULL pointer exception.

(2). Check for exceptions (checked excetion): Exceptions generated during program compilation.

01:ioexception--. IO exception

02:eofexception--. accidentally gets the end of the file, gets the failure

03:filenotfoundexception--file does not exist exception

There is no exception for the 04:classnotfoundexception--class.

2.error--Error

(1): Virtualmachineerror.

01:outomemoryerror

02:stactoverflowerror.

(2): Awterror:

Three. Try: Catch.. Finally

Use of 1.try

(1): Try blocks cannot be used alone, and must be used in conjunction with a catch or finally.

(2): The code for which the exception may have occurred.

(3): If an exception does not occur in a try code block, there is finally a case where finally is executed and then the code after the finally is executed.

If an exception occurs in the try code block, the catch is first matched to the exception, and if the match succeeds, the corresponding catch is executed.

Otherwise, the finally is executed and the program terminates!!!

Use of 2.catch

(1): Each catch block is a subclass that captures and processes a particular exception or the exception!

(2): catch (ArithmeticException ex) defines the type of exception

(3): Multiple simultaneous occurrences, but cannot be repeated

(4): Exception types in multiple catch code blocks must be written in small to large order

(5): Our exception will find a first match from multiple catch blocks and then hand it to this block of code to process

(6): The variables in each catch code block are independent and other blocks of code are inaccessible.

(7): If all the catch code blocks do not match, first go to Finally, then terminate the program.

(8): If no exception occurs in the try, all catch blocks are matched.

(9): Multiple exception declarations are supported in a catch in the java1.7 version.

Use of 3.finally:

(1): No matter what happens, finally will execute

(2) If there is no exception in the try, but there is a return, then the return is executed first.

(3) if system.exit (int status) is present in the code, then finally is not executed

System,exit (0), exit normally.

System.exit (not 0): abnormal exit.

(4) Do some cleanup work, close the stream, release the database connection.

Four: Throw throws

1:throw

(1) The type of exception thrown can be exception or its subclasses!

(2) A throw statement can only throw an exception object.

(3) Throw can only be present in the method body

(4) We have two ways of handling the exception thrown by the throw.

01:try. Catch.. Finally

02.throws

2.throws

(1) throws is declared after the name of the method.

(2) throws can declare multiple, separating each exception with commas,

(3) throws declares an exception that indicates that the current method may have an exception and the caller of the method handles the exception

(4) If Shu Yong throws is in main function main, it is handled by our JVM.

Five. Abnormal chain.

1 What is an anomaly chain?

(1) The anomaly chain refers to wrapping the caught exception in a new exception, which records the original exception's information and re-throws the exception handling.

VI: Custom Exception:

1 Exception class must be a subclass of Throwable

2: Define run exception inheritance RuntimeException

3. Define the inherited exception of the exception being checked.

Seven: The use of the exception principle:

1. Exceptions can only be used in unusual situations.

2: Provide documentation for the exception.

3. Avoid exceptions as much as possible, especially at run-time exceptions

4: Keep The atomicity of the anomaly.

5. Avoid too large try code blocks.

6: Make specific exception types in catch

7: Do not ignore the caught exception in the catch block.

Java public class -04-exception

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.