Black Horse programmer _ exception

Source: Internet
Author: User
Tags stack trace throwable

------- Android training, Java training, and hope to communicate with you! ----------

  • What is an exception?

An exception is an error that occurs when the Java program is running.

For example, a program needs to open a non-stored file, network connection interruption, Operation Array out-of-bounds, and load a non-existent class.

 

Exception cause: the problem is also a specific thing in real life. It can also be described in the form of Java classes. And encapsulated into objects.

In fact, it is the embodiment of the object after Java describes the abnormal situation.

Exception category

Error)

Throwable is generated and thrown by the Java Virtual Machine, including dynamic link failure and virtual machine error. The program does not process it.

Exception)

The parent class of all Exception classes. The subclass defines a variety of possible exception events. Generally, You need to explicitly declare to throw or capture them. Operation Array out of bounds

 

Runtime exception (runtime exception)

A special type of exception, such as Division by 0 and out-of-bounds array badge. It is frequently generated and difficult to handle. If you process it each time, it will significantly affect the readability and Running Efficiency of the program. Therefore, the system detects and sends them to the default exception handling program, you do not have to process it. This type of exception is not handled, and no error is reported during compilation. An exception is reported only when an error occurs during runtime. Therefore, we call it a runtime exception. All runtimeexception subclasses are runtime exceptions. We can also handle runtime exceptions.

Exception during compilation

Except for the runtimeexception subclass, all other exceptions must be handled. if not handled, an error will be reported during compilation. These exceptions are called compile-time exceptions.

Abnormal usage

Exception Handling ------ Java provides special statements for processing.

In the program, you can use the throws keyword declaration after the method to throw an exception.

For compilation exceptions, we usually need to use try ...... Catch statement

Finally can be combined with try ...... Catch usage, exception occurs, and the code in finally will also be executed

Try

{

Code to be detected;

}

Catch (exception variables)

{

Code for exception handling (Handling Method)

}

Finally

{

Statements that will be executed;

}

Some details about the callback exception

If multiple exceptions are declared in the parent class method, rewrite (overwrite) This method can only throw one subset of those exceptions, that is, the subclass cannot throw more exceptions than the parent class.

How to handle multiple exceptions

Use of try statement and finally nesting

Throwable

 

Throwable indicates objects that can be thrown in Java. It is the parent class of all errors and exceptions.

Throwable has two subclasses: Error and exception.

Error indicates an error

Unknown exception indicates an exception.

Ÿ runtimeexception indicates a runtime exception, which is a subclass of exception.

Throws exception // this function can be declared using the keyword throws.

 

System. Out. println (E. getmessage (); // print the exception information/by zero;

System. Out. println (E. tostring (); // Exception name: exception information.

E. printstacktrace (); // Exception name, exception information, and location of the exception. (The most comprehensive information)

// In fact, the default Exception Handling Mechanism of JVM is to call the printstacktrace method. Print stack trace information

// Print the trace information of the abnormal stack.

L declare an exception on the function.

This improves security and allows calling and processing. Compilation fails.

 

L handle multiple exceptions.

 

1. We recommend that you declare more specific exceptions when declaring exceptions. This process can be more specific.

2. When the other party declares several exceptions, it should have several catch blocks. Do not define unnecessary catch blocks.

If an exception in multiple catch blocks has an inheritance relationship, the parent exception catch block is placed at the bottom.

When performing catch processing, the catch must define a specific processing method.

Do not simply define e. printstacktrace (),

Do not simply write an output statement.

 

  • An error occurred while customizing the volume.

N definition: because the project has special problems, these problems are not described by Java and the objects are encapsulated. Therefore, we can follow Java's philosophy of problem encapsulation for these special problems.

Unique issues. Perform custom exception encapsulation.

 

L an exception can be defined by inheriting the exception class

To define a runtime exception, you must inherit the runtimeexception class.

 

Custom exception:

It must be a custom class that inherits exception.

 

  • Cause of inherited exception:

The exception system has one feature: exception classes and exception objects are thrown.

They are both flexible. This flexibility is unique in the throwable system.

 

Only classes and objects in this system can be throws and throw operations.

Difference between throws and throws

Throws are used in functions.

Throw is used in the function.

 

The exception class followed by throws. Can be followed by multiple. Separated by commas.

Throw is followed by an exception object.

 

L finally code block: defines the code to be executed. Finally stores the code that will be executed.

It is usually used to close resources. Finally is not executed in only one case. When it is executed to system. Exit (0); fianlly will not execute.

 

Three formats of the processing statement:

First format:

// Remember: Catch is used to handle exceptions. If no catch is found, the exception has not been processed. If the exception is detected. It must be declared.

 

  • Exceptions are embodied in child parent class overwrite;

1. When a subclass overwrites the parent class, if the method of the parent class throws an exception, the override method of the subclass can only throw the exception of the parent class or the subclass of the exception.

2. If the parent class method throws multiple exceptions, the Child class can only throw a subset of the parent class exceptions when overwriting the method.

3. If no exception is thrown in the parent class or interface method, the subclass cannot overwrite the method.

If the subclass method has an exception. Try. It cannot be discarded.

Black Horse programmer _ exception

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.