Java standard exceptions

In this section we introduce the Java standard exceptions:

Throwable Java class is used to represent anything that can be thrown as an exception class.

Throwable objects can be divided into two types:

  1. Error and system used to represent a compile-time error (except in special circumstances, you generally do not have to care about)

     2.Exception are the basic types can be thrown, possibly throwing Exception abnormalities in Java class libraries, methods, and user run-time failures.

Therefore, the base type Java programmers are usually concerned with Exception.

In unusual learning process, we should pay attention to understand the concept and how to use. Because the number of Java exception in the sustained increase, and third-party libraries will have its own exception.

Special case: RuntimeException

  

if(t == null)
     throw new NullPointerException();

In this example, if each must be passed to the method of references to check whether it is null, this is a very boring thing.

However, this does not require us to do the programmer, which is part of the detection of the standard Java runtime.

If we need to call a null reference, Java will automatically throw a NullPointerException, then the above code is superfluous.

 

In fact, the type of anomaly belonging to run a lot, they will automatically be thrown Java virtual machine, so we do not have an exception stating that they will be listed.

These exceptions are inherited from the class RuntimeException, which constitute a group having the same types of abnormal behavior and characteristics. It is important that we no longer need to declare the exception description method throws RuntimeException types of exceptions, they especially are called "unchecked abnormal." This anomaly is wrong, it will be automatically captured.

Then we would think, if they do not capture this anomaly, what happens?

We can go to understand from this example:

public class NeverCaught {
	static void f() {
		throw new RuntimeException("From f()");
	}
	static void g() {
		f();
	}
	public static void main(String[] args) {
		g();
	}
}

 

operation result:

analysis:

So for this type of exception, the compiler does not need to be described abnormality, but the output is reported to System.err.

So: RuntimeException if not caught and direct access to main (), it will be called before the program exits printStackTrace anomalies () method.

We must bear in mind: You can only ignore RuntimeException (and its subclasses) type of exception in the code, exception handling other types are enforced by the compiler. The reason: RuntimeException is represented by programming errors:

1) unexpected error. For example, you transfer from outside the control of incoming null reference.

2) As a programmer, the error should be checked in the code.

It is worth noting: should not the Java exception handling mechanism as a single-purpose tool. It is used to handle a number of annoying runtime errors, and these errors are often caused by factors outside the control of the code; however, he found that for some programming errors can not be detected by the compiler, it is also very important.

Guess you like

Origin blog.csdn.net/qq_41026809/article/details/92424381