How to implement Java exception

The exception in

Java is divided into runtime exception ( RuntimeException ) and check exception ( checked exception )
for many exceptions, statements like throw new Exception () declare an exception in the program.
if it is a runtime exception, it can be handled in the program or not. For checking an exception, you must handle it
. There is a problem, that is, how do you throw an exception when it is not declared? Like

obj.method();

when obj==null , NPE is definitely thrown, but there is no such statement

in method () .
method{
    ifthis==null
     throw new NullPointException();
}

how is a Exception like this implemented

?

is it done in JVM ?
so if you did it in JVM , then JVM did this for which check exceptions, and how did JVM do it?

@ Jian Xin is right. The problem here is really obvious. Obj is empty, and there is no way to find method.
but I also want to know how the NullPointException is thrown when "null.method ()", and the Java code doesn"t see where the NullPointException? is thrown.

Mar.29,2021

how is obj.method () called? Do you have to first find the memory address of obj, get the type of obj (or something you can recognize), then find the offset address of method, and call method. When obj is null, you can't even find method, so how can you throw an exception in method?
refer more to other people's blogs https://blog.csdn.net/qq_3161.

PS: if you are interested in understanding the underlying layer, you can try to convert java into assembly and see how it is implemented.


Hello. To put it simply, JVM does not handle exceptions and generally hangs up directly, so there is an exception handling mechanism. If exceptions are subdivided into Error and Exception, there are also non-check exceptions and check exceptions, run-time exceptions and non-run-time exceptions. Under our vernacular explanation, exception running out is the corresponding thread and type. Java application exception handling mechanism: throwing exception, catching exception.
has one point: the exception that a method can catch must be the exception that Java code throws somewhere. In a nutshell, exceptions are always thrown first and caught later.
reference:
java
talk about java exception
Cainiao-java anomaly Analysis

Menu