Java exception schema diagram and polygon questions

Source: Internet
Author: User
Tags try catch

The red Check for exceptions is whether eclipse will prompt you for a try catch or throws.

Non-check exception is/0,nullpointexception, data out-of-bounds access Indexofoutbounds exception

Error: The most common oom error, as well as Classnotfoundeerror,stackoverflowerror :

The question generalization of Java exception processing surface

1. What is the difference between error and exception?

Error indicates system-level errors, is a Java runtime internal error or a hardware problem, can not expect the program to handle such a problem, in addition to exiting the run has no choice, it is thrown by the Java virtual machine.

Exception indicates that the program needs to catch, the exception that needs to be handled, is the problem with the imperfect program design, the problem that the program must deal with

2. What is the difference between a run-time exception and a general exception

Java provides two main types of exceptions: RuntimeException and Checkedexception

General exceptions (Checkedexception) mainly refer to IO exceptions, SQL exceptions, and so on. For this exception, the JVM requires that we have to cathc it, so, in the face of this anomaly, whether we want to

, it's all about writing a lot of catch blocks to handle the exceptions that might occur.

Run-time exception (runtimeexception) we generally do not handle, when such an exception occurs when the program is taken over by the virtual machine. For example, we have never dealt with nullpointerexception, and

This exception is also one of the most common exceptions.

In the event of a runtime exception, the program throws the exception up and down until it encounters the processing code, and if there is no catch block to process, to the top, if multithreading is Thread.run () thrown, as

If the result is not multithreaded then it is thrown by Main.run (). After the throw, if it is a thread, then the thread is terminated, if it is the main program, then the program is terminated.

In fact, the run-time exception is also inherited from exception, can also be used to handle the catch block, but we generally do not handle it, that is, if the runtime exception is not catch processing, then the result is not

Either the thread exits or the main program terminates.

If you do not want to terminate, then we must catch all possible run-time exceptions. If the exception data appears in the program, but it does not affect the following program execution, then we should be in the catch block will be different

Data is discarded, and then logs are logged. If, it affects the following program to run, then it is better to quit the program.

3. The principle of exception handling mechanism in Java

Java handles exceptions in an object-oriented manner, and Java classifies exceptions according to different types and provides a good interface. In Java, each exception is an object, and it's all throwable

Or an instance of its child class. When an exception is thrown, a method throws an exception object that contains the exception information, and the method that invokes the object can catch the exception and handle the exception. Java's

Exception handling is achieved by 5 keywords: try catch throw throws finally.

In general, the execution of a program with a try, if an exception occurs, the system throws (throws), we can catch it by its type, or finally by the default processor to process it (finally).

Try: A program used to specify a block to prevent all exceptions

Catch: Immediately following a try to catch an exception

Throw: Used to explicitly throw an exception

Throws: Used to indicate a variety of exceptions that a member function might throw

Finally: A piece of code that ensures that a piece of code will be executed regardless of what exception occurs.

4, how do you usually deal with the exception in the project.

(1) Try to avoid the appearance of runtimeexception. For example, for code that might appear with a null pointer, be sure to determine if the object is empty before using it, and when necessary, runtimeexception

Try catch processing is also performed.

(2) When doing a try catch processing, the exception information is recorded in the catch code block, by calling the exception class related methods to obtain information about the exception, return to the web side, not only to the user good

The user experience, but also to help the programmer to locate the location of the exception and the cause of the problem. For example, a project that was previously made, the program encounters an exception page displays a picture that tells the user what actions caused the program to appear

Something unusual, and there is a button on the picture to click on to show the details of the exception for the programmer to see.

5, final, finally, finalize the difference

(1), Final used to declare variables, methods, and classes, respectively, to indicate that the value of the variable is immutable, the method is not overwritten, the class can not inherit

(2), finally is a keyword in the exception processing, indicating that the code inside the finally{} must be executed

(3), Finalize is a method of the object class that will invoke this method of the reclaimed object at garbage collection time.

6, try () there is a return statement, then the following finally{} inside the code will not be executed, when execution, is in return before or after return?

I wrote a code to test a bit:

Display output is 1, remember it, do not want to drill the problem of the horn tip, there is no big use.

Java exception schema diagram and polygon questions

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.