Java - try 和 catch 异常处理如何运行
Java - how try and catch exception handling runs
我几乎是 Java 编程的新手,我正在尝试了解 try 和 catch 异常处理是如何运行的。
我的疑问是,当我创建自定义异常 class 并将其放入主 class 的 try 块中时,我可以在没有引用的情况下实例化它,但是当我编写 catch 块我可以使用与实例化异常相同类型的成员 class 而无需实例化它。
例如:
public void main.....{
.....
try{
if(division==0){
throw new DivisionByZero() ;
//Divisionbyzero is my customized exception class
}
}
catch(DivisionByZero e) {
e. methodDivisionByZero;
}
}
重点是我可以直接使用e成员,不用new实例化。
e 是否在我抛出异常之前以某种方式实例化(即使我没有在 try 块中编写任何引用)?
提前感谢您的回答。
您正在创建一个新对象:
throw new DivisionByZero() ;
Throwing/Catching 只是一个 "mean of transportation"。在"essence"中,就像写成:
DivisionByZero dbz = new DivisionByZero() ;
dbz.someMethod();
你创建一个异常,你抛出 "it",你捕获 "it" 然后 "it" 只是对某个对象的引用。
当抛出异常时,将创建一个异常对象,并且不会在 try 块中执行更多行。这个异常对象与catch块参数匹配最合适的接收参数变量中的对象,就像方法参数一样。在这种情况下 catch(DivisionByZero e)
正确的 catch 子句应该是 catch(ArithmeticException e)
以捕获除以零的异常,参数 e
是对异常对象的引用。
对于你的第一个问题我可以在没有引用的情况下实例化它:没有必要将实例分配给任何引用变量。我们使用引用变量,以便我们将来可以使用它的成员。
而对于第二个问题我可以使用同类型的实例化异常成员class而不用实例化它 : 当你写
throw new DivisionByZero() ;` this is equal to
DivisionByZero dBZException = new DivisionByZero() ;
throw dBZException;
这意味着您正在将一个实例传递给您拥有 class DivisionByZero
.
引用变量的 catch 块
因此在 catch 块中,您的代码是这样工作的:
DivisionByZero e = dBZException;
希望现在已经清楚了。
The point is that I can use directly the e member without instantiate it by means of new.
好吧,这里不需要任何实例,在 catch
块中,就像在方法调用中一样,我们传递一个 **[reference][1]**
,这里是对应该处理的异常的引用。
你可以看一下 The catch Blocks Oracle Docs :
Each catch block is an exception handler that handles the type of exception indicated by its argument. The argument type, ExceptionType, declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class. The handler can refer to the exception with name.
我几乎是 Java 编程的新手,我正在尝试了解 try 和 catch 异常处理是如何运行的。
我的疑问是,当我创建自定义异常 class 并将其放入主 class 的 try 块中时,我可以在没有引用的情况下实例化它,但是当我编写 catch 块我可以使用与实例化异常相同类型的成员 class 而无需实例化它。
例如:
public void main.....{
.....
try{
if(division==0){
throw new DivisionByZero() ;
//Divisionbyzero is my customized exception class
}
}
catch(DivisionByZero e) {
e. methodDivisionByZero;
}
}
重点是我可以直接使用e成员,不用new实例化。 e 是否在我抛出异常之前以某种方式实例化(即使我没有在 try 块中编写任何引用)?
提前感谢您的回答。
您正在创建一个新对象:
throw new DivisionByZero() ;
Throwing/Catching 只是一个 "mean of transportation"。在"essence"中,就像写成:
DivisionByZero dbz = new DivisionByZero() ;
dbz.someMethod();
你创建一个异常,你抛出 "it",你捕获 "it" 然后 "it" 只是对某个对象的引用。
当抛出异常时,将创建一个异常对象,并且不会在 try 块中执行更多行。这个异常对象与catch块参数匹配最合适的接收参数变量中的对象,就像方法参数一样。在这种情况下 catch(DivisionByZero e)
正确的 catch 子句应该是 catch(ArithmeticException e)
以捕获除以零的异常,参数 e
是对异常对象的引用。
对于你的第一个问题我可以在没有引用的情况下实例化它:没有必要将实例分配给任何引用变量。我们使用引用变量,以便我们将来可以使用它的成员。
而对于第二个问题我可以使用同类型的实例化异常成员class而不用实例化它 : 当你写
throw new DivisionByZero() ;` this is equal to
DivisionByZero dBZException = new DivisionByZero() ;
throw dBZException;
这意味着您正在将一个实例传递给您拥有 class DivisionByZero
.
因此在 catch 块中,您的代码是这样工作的:
DivisionByZero e = dBZException;
希望现在已经清楚了。
The point is that I can use directly the e member without instantiate it by means of new.
好吧,这里不需要任何实例,在 catch
块中,就像在方法调用中一样,我们传递一个 **[reference][1]**
,这里是对应该处理的异常的引用。
你可以看一下 The catch Blocks Oracle Docs :
Each catch block is an exception handler that handles the type of exception indicated by its argument. The argument type, ExceptionType, declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class. The handler can refer to the exception with name.