简体   繁体   English

Java中的异常:使用throw或try-catch

[英]Exceptions in Java: Using throws or try-catch

This is a very well discussed topic. 这是一个讨论得很好的话题。 Out of many links, I did find the below two useful. 在许多链接中,我确实发现以下两个有用。

1 , 2 12

My questions is this: 我的问题是这样的:

  1. I am writing multiple methods, some for general purposes- say, writing into file, reading from file, splitting a string using some regex. 我正在编写多种方法,其中一些用于一般用途-例如,写入文件,从文件读取,使用某些正则表达式拆分字符串。

  2. I am calling these methods from different parts of my application- say class1, class 2 and class 3 calls the file writing method, but with different file names and data. 我从应用程序的不同部分调用这些方法-例如class1,class 2和class 3调用文件写入方法,但是具有不同的文件名和数据。

Where should I include try catch and throws? 我应该在哪里包括罚球? 1. The outermost method, say the main, from where I am calling the class1, class2 and class 3 methdods have one set of try, catch and use throws in the innermost functions. 1.最主要的方法,例如主要的方法,我从那里调用class1,class2和class 3方法在最里面的函数中具有一组try,catch和use throws。

  1. Or, have a try catch in the innermost-None to the outer files.. 或者,尝试将最里面的文件捕获到最外面的文件。

Which is a better option and why? 哪个是更好的选择,为什么? Are there any other ways of dealing with this. 还有其他方法可以解决这个问题。

Thanks 谢谢

The heart of this matter is how to decide logically where your exceptions need to be handled . 这件事情的核心是如何逻辑地确定需要处理您的异常的地方

Let's take an example. 让我们举个例子。 A method that takes a File object and performs some operation. 一种采用File对象并执行某些操作的方法。

public void performFileOperation(File f) throws FileNotFoundException{
    // Perform logic.
}

In this case, chances are that you want the class/method calling this method to handle the Exception . 在这种情况下,您很可能希望调用此方法的类/方法处理 Exception If you had a try/catch block in the performFileOperation() method, then the method calling the performFileOperation() method would never know if the operation failed (and therefore can't know what to do about it). 如果您在performFileOperation()方法中有一个try / catch块,那么调用performFileOperation()方法的方法将永远不会知道操作是否失败(因此无法知道该怎么做)。

It is really a matter of where it makes sense to handle the Exception that is thrown. 实际上,在哪里处理抛出的Exception才有意义。 You need to work out which sections of your code need to know that an exception has occurred , and use try/catch blocks there. 您需要弄清楚代码的哪些部分需要知道发生了异常 ,然后在其中使用try / catch块。 If the next level up needs to know as well, then the catch block can throw the Exception so the method further up the stack can know about and handle it as well. 如果还需要进一步了解下一层,则catch块可以抛出Exception因此堆栈Exception的方法也可以知道并处理该Exception

Another good way to look at it is that most exceptions need to be handled. 另一种看待它的好方法是,大多数异常都需要处理。 Something needs to be done about them. 关于它们,需要做一些事情。 If your method is not in a position to do something significant to handle that exception then it needs to throw the Exception until it makes it's way to somewhere that can meaningfully handle (read, do something about) it. 如果您的方法无法做一些有意义的事情来处理该异常,那么它需要throwException直到它进入可以有意义地处理(读取,处理)该Exception的地方。

I hope this helps. 我希望这有帮助。

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

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