Android NoteX Q11: Can trycatch(Exception e) catch all exceptions

Of course not, for example: loading a library that does not exist.

System.loadLibrary("c++_shared");

Still get an error.

java.lang.UnsatisfiedLinkError: dlopen failed: library “libc++_shared.so” not found
at java.lang.Runtime.loadLibrary0(Runtime.java:1087)
at java.lang.Runtime.loadLibrary0(Runtime.java:1008)
at java.lang.System.loadLibrary(System.java:1664)

So how to catch such errors?
Use trycatch(Throwable e).

Review the difference between Throwable, Exception, and Error.
The latter two are subclasses of the former.
Exception : It is often used as an exception that can be handled by program means. It usually occurs at runtime and can be handled.
Error : It often refers to a serious error in the program. This exception will be found in some compilation stages, such as NoClassDefFoundError, Virtual MachineError, etc., and some occur during runtime: OutOfMemoryError.

So this type is very clear, "library * not found" is a serious error, the library was not found, so it does not belong to the scope of Exception. Since it is a serious error, it is better not to trycacth, it is better to solve it.

Guess you like

Origin blog.csdn.net/ganshenml/article/details/118344446