简体   繁体   English

Java - 如何尝试和捕获异常处理运行

[英]Java - how try and catch exception handling runs

I'm almost totally new to Java programming and I'm trying to understand how try and catch exception handling runs.我对 Java 编程几乎完全陌生,我试图了解 try 和 catch 异常处理是如何运行的。

My doubt is about the fact that when I create a customized exception class and I throw it in a try block of a main class I can instantiate it without reference, but then when I write the catch block I can use a member of the same kind of the instantiated exception class without instantiate it.我的疑问是当我创建一个自定义的异常类并将它扔到主类的 try 块中时,我可以在没有引用的情况下实例化它,但是当我编写 catch 块时,我可以使用相同类型的成员实例化的异常类,而不对其进行实例化。

For example :例如 :

public void main.....{

.....

 try{

    if(division==0){

         throw new DivisionByZero() ;
         //Divisionbyzero is my customized exception class

     } 

 }

catch(DivisionByZero e) {

   e. methodDivisionByZero;

 }

}

The point is that I can use directly the e member without instantiate it by means of new.关键是我可以直接使用 e 成员,而不需要通过 new 实例化它。 Is e somehow instantiate before when I throw the exception (even if I don't write any reference in the try block)?在我抛出异常之前是否以某种方式实例化(即使我没有在 try 块中写入任何引用)?

Thanks in advance for any answer.提前感谢您的任何回答。

When an exception is thrown, an exception object is created and further lines are not executed in the try block.当抛出异常时,会创建一个异常对象,并且不会在 try 块中执行更多行。 This exception object is matched with the catch block parameters the most suitable one receives the object in the parameter variable just like method parameters .此异常对象与 catch 块参数匹配,最合适的方法是接收参数变量中的对象,就像方法参数一样 In this case catch(DivisionByZero e) the correct catch clause should be catch(ArithmeticException e) to catch the divide by zero exception and the parameter e is the reference to the exception object.在这种情况下, catch(DivisionByZero e)正确的 catch 子句应该是catch(ArithmeticException e)来捕获除以零异常,并且参数e是对异常对象的引用。

You are creating a new object:正在创建一个新对象:

throw new DivisionByZero() ;

Throwing/Catching is just a "mean of transportation".投掷/捕捉只是一种“交通工具”。 In "essence", it is like writing:在“本质”上,就像写:

DivisionByZero dbz = new DivisionByZero() ;
dbz.someMethod();

You create an exception, you throw "it", you catch "it" and then "it" is nothing else but a reference to some object.您创建一个异常,抛出“它”,捕获“它”,然后“它”只是对某个对象的引用。

For your first question I can instantiate it without reference : it's not necessary to assign an instance to any reference variable.对于您的第一个问题,我可以在不引用的情况下实例化它:没有必要将实例分配给任何引用变量。 We use reference variables so that we can use it's members in future.我们使用引用变量,以便我们将来可以使用它的成员。

And for the second question I can use a member of the same kind of the instantiated exception class without instantiate it : When you are writing对于第二个问题,我可以使用同类型的实例化异常类的成员而不对其进行实例化:当你在写的时候

throw new DivisionByZero() ;` this is equal to 
DivisionByZero dBZException = new DivisionByZero() ;
throw dBZException;

which means you are passing an instance to the catch block where you have the reference variable of class DivisionByZero .这意味着您将一个实例传递给 catch 块,在那里您拥有类DivisionByZero的引用变量。

Thus in catch block your code works like this :因此在 catch 块中你的代码是这样工作的:

DivisionByZero e = dBZException;

Hope it's clear now.希望现在清楚了。

The point is that I can use directly the e member without instantiate it by means of new.关键是我可以直接使用 e 成员,而不需要通过 new 实例化它。

Well you don't need any instance here, in the catch block like in methods call we pass a **[reference][1]** , here it's a reference to the Exception that should be handled.好吧,这里不需要任何实例,在方法调用中的catch块中,我们传递**[reference][1]** ,这里是对应该处理的 Exception 的引用。

You can see that if you take a look at The catch Blocks Oracle Docs :如果您查看The catch Blocks Oracle Docs ,您会发现:

Each catch block is an exception handler that handles the type of exception indicated by its argument.每个 catch 块都是一个异常处理程序,用于处理由其参数指示的异常类型。 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.参数类型 ExceptionType 声明处理程序可以处理的异常类型,并且必须是从 Throwable 类继承的类的名称。 The handler can refer to the exception with name.处理程序可以使用名称引用异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM