• 周二. 10 月 8th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Java standard exception

King Wang

1 月 3, 2022

In this section we will introduce Java Standard exception :

Throwable This Java Class is used to represent any class that can be thrown as an exception .

Throwable There are two types of objects :

  1. Error Used to indicate compile time and system errors ( Except in special circumstances , Generally, you don’t need to care about )

     2.Exception Is the basic type that can be thrown , stay Java Class library 、 Both user methods and runtime faults can throw Exception Type abnormality .

therefore Java The base types that programmers care about are usually Exception.

In the process of abnormal learning , We should pay attention to understanding the concept and how to use . because Java The number of exceptions in is increasing continuously , And the third-party class library also has its own exceptions .

Special examples :RuntimeException

  

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

In this case , If you have to check that every reference passed to a method is null, It’s a very boring thing .

however , It doesn’t need to be done by our programmers , It is Java Part of the standard runtime detection .

If we need to be right null Reference to call ,Java Will automatically throw NullPointerException abnormal , So the above code is redundant .

 

There are many types of runtime exceptions , They will be automatically Java Virtual machine throw , So we don’t have to list them in the exception description .

These exceptions are from RuntimeException Class , This constitutes a set of exception types with the same characteristics and behaviors . The important thing is that we no longer need to declare in the exception specification that the method will throw RuntimeException Exception of type , They are especially called “ Not subject to inspection ”. This kind of exception is a mistake , Will be automatically captured .

Then we will think , If you don’t catch this kind of exception , What will happen ?

We can learn 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();
}
}

 

Running results :

analysis :

So for this type of exception , The compiler does not need an exception specification , But the output is reported to System.err.

therefore : If RuntimeException Without being caught, go straight to main(), The exception will be called before the program exits printStackTrace() Method .

We must remember : You can only ignore… In your code RuntimeException( And its subclasses ) Exception of type , Other types of exception handling are enforced by the compiler . The reason is :RuntimeException It’s a programming error :

1) An unforeseen mistake . For example, it’s passed in from outside your control null quote .

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

It is worth noting that : Should not put Java As a single-purpose tool . It’s used to handle some annoying runtime errors , These errors are often caused by factors other than code control ; However , He’s concerned about finding programming errors that some compilers can’t detect , It’s also very important .

发表回复