Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java异常程序跟踪_Java_Exception - Fatal编程技术网

Java异常程序跟踪

Java异常程序跟踪,java,exception,Java,Exception,输出: 这是课堂上关于异常的一个例子。我没有编写这个程序,但是我在跟踪这个异常示例程序时遇到了一些问题。我甚至不知道为什么会抛出异常。如果有人能带我看完这个程序,那太好了,谢谢。因为你从你的类中获取了这个程序,我假设他们试图解释异常对象的传播,当他们通过用户定义的处理程序try and catch块处理时,以及当他们被赋予默认异常处理程序JVM时。 1 main方法调用m2方法,m2方法调用m1方法,m1方法由用户定义的处理程序处理。 在这里调用m1方法的2m2方法,不管形成什么异常对象,都被赋

输出:


这是课堂上关于异常的一个例子。我没有编写这个程序,但是我在跟踪这个异常示例程序时遇到了一些问题。我甚至不知道为什么会抛出异常。如果有人能带我看完这个程序,那太好了,谢谢。

因为你从你的类中获取了这个程序,我假设他们试图解释异常对象的传播,当他们通过用户定义的处理程序try and catch块处理时,以及当他们被赋予默认异常处理程序JVM时。 1 main方法调用m2方法,m2方法调用m1方法,m1方法由用户定义的处理程序处理。 在这里调用m1方法的2m2方法,不管形成什么异常对象,都被赋予其下面的层次结构,该层次结构将捕获m1方法中引发的异常对象。 3没有用户定义处理程序的主调用m2方法,该方法将转到默认异常处理程序,即jvm。
它们只是演示异常对象如何在堆栈层次结构中流动

在m1中抛出异常,您有:

m1 : print this line....
m2 :hello exception from m1()!
m2 : resume here
m1 : print this line....
main :hello exception from m1()!
main : resume here
m1 : print this line....
m2 :hello exception from m1()!
m2 : resume here
m1 : print this line....
Exception in thread "main" java.lang.RuntimeException: hello exception
from m1() 
然后通过调用堆栈传回,直到它们可以由适当的catch块处理。程序通常通过识别捕获异常的方法来处理异常,然后打印消息

至于逻辑,这似乎只是一个学术或抽象的示例,因此抛出异常只是为了演示如何实现自定义异常和异常处理,以及异常如何影响程序流


程序注释写得很得体,因此任何进一步的解释都有点多余,但要想更完整地解释异常,请尝试。

自行抛出异常并询问它们抛出的原因不是一件好事。抛出新的RuntimeException Hello exception from m1!;这是程序中的罪魁祸首。这总是会从m1引发异常,您可能希望添加一个条件,以确定它何时引发异常Corry我应该更清楚。这是一个类内异常示例。我只是在跟踪程序和理解为什么需要异常/为什么抛出异常时遇到问题。试试看
m1 : print this line....
m2 :hello exception from m1()!
m2 : resume here
m1 : print this line....
main :hello exception from m1()!
main : resume here
m1 : print this line....
m2 :hello exception from m1()!
m2 : resume here
m1 : print this line....
Exception in thread "main" java.lang.RuntimeException: hello exception
from m1() 
throw new RuntimeException("hello exception from m1()!");