简体   繁体   English

哪种方法更适合异常处理

[英]Which way is better for exception handling

Which is a better approach to exception handling and why?: 哪种是处理异常的更好方法,为什么?:

(1) Defining a single exception for the whole application which takes a string message and displays it. (1)定义整个应用程序的单个异常,该异常将接收字符串消息并显示它。 Use this exception everywhere with a specific message appropriate to the scenario. 在任何地方都可以使用此异常,并提供适合该情况的特定消息。

I have no idea why this is not appropriate. 我不知道为什么这不合适。

(2) Defining a new exception class for each different case in the application. (2)为应用程序中的每个不同情况定义一个新的异常类。

I feel this is not appropriate because there are cases where the exception is at just one place in the application. 我认为这是不合适的,因为在某些情况下,异常仅在应用程序中的一个位置出现。 eg amount entered is -ve Is it ok to create a whole new exception class just for a single case in the app ? 例如,输入的金额为-ve是否可以仅针对应用程序中的单个案例创建一个全新的异常类?

Definitely defining new exception for all different cases. 绝对为所有不同情况定义新的例外。

Imagine you are implementing an interpreter/compiler. 假设您正在实现解释器/编译器。 You gonna have modules as SemanticVerifier, Interpreter, CodeGenerator (and others of course). 您将拥有诸如SemanticVerifier,Interpreter,CodeGenerator(当然还有其他)的模块。 You want to know what kind of exception gonna be thrown when your program crash. 您想知道程序崩溃时将引发哪种异常。 Oh there it's Semantic! 哦,这是语义的! there look it's a bug in the Interpreter! 那里是口译员的错误! And you want the user to know so he can report a more precise bug. 您希望用户知道,以便他可以报告更精确的错误。

I personally think it leads to a better design if you have separate exceptions for every module. 我个人认为,如果您对每个模块都有单独的例外,则可以带来更好的设计。

Define a new exception for each kind of error that matters to your user . 为与用户相关的每种错误定义新的异常。 So, for example, ideally you should catch NullPointerException inside your program, and turn it into a CustomerLookupException to that the user level of the program can report "Software error retrieving customer." 因此,例如,理想情况下,您应该在程序内部捕获NullPointerException,并将其转换为CustomerLookupException,以使程序的用户级别可以报告“检索客户的软件错误”。 Exception chaining is handy for this, as you can pass along the original exception as well. 异常链接对此非常方便,因为您也可以传递原始异常。

The whole Java Tutorial on exceptions is a good resource. 整个有关异常的Java教程是一个很好的资源。

You should define a new exception for each case. 您应该为每种情况定义一个新的例外。

The reason is so that each Exception can be handled independently. 原因是可以单独处理每个Exception

For example, catching the Exception s differently allows you to either continue in some cases, or end in others. 例如,以不同的方式捕获Exception可以使您在某些情况下继续执行,或在其他情况下结束。

in my view your 1st option is more suitable 我认为您的第一种选择更适合

(1) Defining a single exception for whole application which takes a string msg and displays it. (1)为整个应用程序定义单个异常,该异常采用字符串msg并显示它。 And using it at all places with appropriate place specific message. 并在所有地方使用适当的地方特定消息。

Even i am using a single exception in the application. 甚至我在应用程序中使用单个异常。 If you create separate exception for each type it will increase complexity. 如果为每种类型创建单独的异常,则会增加复杂性。 It will have lot of redundant code. 它将有很多冗余代码。 For denoting different type of exception, you can pass a parameter(type or something) in the common exception itself. 为了表示不同类型的异常,您可以在通用异常本身中传递参数(类型或其他内容)。

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

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