The Java self-study road-DAY14

Source: Internet
Author: User
Tags array definition throw exception throwable try catch

JAVA14

Exception Introduction

The problem with Java code that occurs during runtime is an exception

L encapsulate exception information in Java as a class

• When a problem occurs, an exception class object is created and the exception-related information is thrown

L If the location of the abnormal occurrence of reasons, etc.

Inheritance system

L Java.lang
Class Throwable can be thrown out of class

L All errors and exceptions in Java

L known sub-class directly:

Error, Exception

Differences between exceptions and errors

L array access out of bounds is an exception

Exception RuntimeException

Compile, can be handled in a specific way during runtime

L array space too large is wrong

Error

An error occurred during Operation no specific method of handling system-level issues are occurring in the system where the JVM resides, and feedback to the JVM can only modify the code

Cause of the exception and how to handle it

L

L method Internal problem The JVM has detected an exception to the object that created the corresponding exception

The L method cannot handle the caller who throws the exception object to the method inside

The caller of the L method cannot handle the object and throws it to the caller of the main method.

L is the JVM virtual machine and the JVM receives the exception

L then the JVM outputs the exception information in a red font to the console to stop the program at the calling method code

Handling Throw keyword Processing

L Use the method to consider the occurrence of anomalies need to determine the parameters passed to see if it is legal if it is illegal to tell the caller, it is necessary to throw an exception in the way to tell

L Throw an exception inside a method

l Example

    1. Perform a judgment throw exception throw new Exception ("info")
    2. Declare exception after method name with throws exception type
    3. method calls should take into account the method of the exception of the call when the exception to the method of processing, such as throwing exceptions to the upper level such as the main method thrown to the virtual machine

Try ... catch...finally processing

L format

Try {potentially unexpected code} catch {exception handling} finally {code that must be executed}

l Example

    1. The main method executes the array definition
    2. Enter try code block
    3. Calling methods
    4. Method execution judgment null create your own well-written exception class object
    5. Throwing an exception object to a program that is behind a method caller no longer executes
    6. The method caller's try detected an exception occurred and received an exception object
    7. Throws the exception object to the catch code block to handle the exception
    8. Catch catch Capture
    9. Catch can catch a corresponding type of exception
    10. Catch catches to this exception for processing
    11. Exception handler continues execution
Multiple catch capture

L can handle multiple exceptions

L Precautions

    1. Catch-write exceptions have sequential concepts
    2. Peer exception: No inheritance relationship between thrown exception classes
    3. There is no order for peer exceptions
    4. Ctrl-T Inheritance tree
    5. The exception of the subordinate relationship must be the higher the parent class is written below
    6. Otherwise the compilation will error
    7. Low-level catch is useless.

Finally code block

L code that must be executed

Execute the block of code, whether or not an exception occurs

L used to release resources

Try...catch ... and throws how to choose

L used a method of throwing an exception to use Try...catch ...

l Otherwise, it will cause the program to stop if it is thrown upwards.

Compile exception and run-time exception

L Compile exception

    1. Calling a method that throws an exception fails to process the compilation (try throws)

L Run exception

    1. The exception that is thrown is the RuntimeException class or his subclass, which are run-time exceptions.
    2. What is thrown inside a method is a run exception and does not require a throws declaration
    3. The caller also does not need to process
    4. Design reasons

A) operation exception cannot occur

b) But if the programmer needs to stop the program to modify the source code

c) Run exception if the following code is not meaningful

Case

L Calculate circular Area

L to invoke run-time exception handling with a radius of 1

Exception handling When overridden by a method

L The method of the parent class throws an exception

    1. Subclasses can not throw exceptions after overriding
    2. You can also not throw exceptions
    3. If the subclass throws an exception, the exception object cannot be greater than the exception of the parent class
    4. Subclasses also cannot throw a peer exception
    5. Reason

The parent class calls the original method of the parent class when a polymorphic call method

    1. The parent class throws an exception subclass and cannot throw an exception
    2. The subclass method throws an exception when the method in the parent class is called to throw an exception that can only be thrown with a try catch and cannot be thrown up because the parent class does not throw an exception
Throwable class method

L String GetMessage ()

A detailed description of the exception information

L String toString ()

A short description of the exception information

L Void printstacktrace () virtual machine default calling method

Tracing the exception information to the standard error stream

l Example

Custom Exception Ingestion

Java pre-defined a lot of exceptions

l have some exceptions.

l need to write it myself.

L For example negative scores

L Format xxxexception

L Custom exception classes need to inherit the parent class exception

Example

L Calculate the average of the scores. Abnormal stop operation with negative number

L

L

L

L Most use Run-time exceptions

l need to write null parameter constructor with parameter constructor

L Call the parent constructor with super in the parameter constructor

The Java self-study road-DAY14

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.