简体   繁体   English

我应该在程序中抛出异常还是打印错误语句?

[英]Should I throw an exception or print out an error statement in a program?

I have my program working and all done (java).我的程序正在运行并且全部完成(java)。 It's a short and easy program for a job interview.这是一个简短而简单的求职面试程序。 I handle stuff like improper input format by throwing a custom exception.我通过抛出自定义异常来处理诸如不正确的输入格式之类的事情。 Is that the best way to do it or should I just make a print statement?这是最好的方法还是我应该做一个打印声明?

Exceptions are only useful if they will be handled by other code. 只有在其他代码处理异常时,异常才有用。

If you're writing a reusable library, you should by all means throw an exception. 如果您正在编写可重用的库,那么您应该抛出异常。
There is nothing more frustrating than calling a third-party library that logs errors to the console instead of telling your code about them. 没有什么比调用第三方库将记录错误记录到控制台而不是告诉您的代码更令人沮丧的了。

However, if you're writing a standalone utility, it's nicer to print friendly error messages than an ugly stack trace. 但是,如果您正在编写一个独立的实用程序,那么打印友好的错误消息比使用难看的堆栈跟踪更好。

The most flexible approach is to write reusable code that throws exceptions, then add catch blocks in main() (or elsewhere in the standalone portion) that prints friendly messages. 最灵活的方法是编写抛出异常的可重用代码,然后在main() (或独立部分的其他位置main()添加catch块,以打印友好消息。

  • If you handle improper format inline is the code readable? 如果处理不正确的内联格式是代码可读吗? If so - fine, if not - throw an exception and handle it elsewhere 如果是这样 - 如果没有 - 罚款 - 抛出异常并在其他地方处理它
  • Are you able to handle improper format properly in the place you are parsing it or maybe some more generic method/class/module is actually calling your routine and should decide what to do? 您是否能够在解析它的地方正确处理不正确的格式,或者某些更通用的方法/类/模块实际上是在调用您的例程并且应该决定该怎么做? If the latter is the case -> throw an exception 如果是后者 - >抛出异常

In general - it depends. 一般来说 - 这取决于。 If you can handle this special situation "inline" - you can do it (make sure it's readable). 如果你能处理这种“内联”的特殊情况 - 你可以这样做(确保它是可读的)。 If not - throw an exception. 如果不是 - 抛出异常。

Here's a good reference on exception best practices . 这是关于异常最佳实践的一个很好的参考。 You should make sure you are following these. 你应该确保你遵循这些。

In your particular case (based on the details you have provided) a user may upload/select a file that has bad data. 在您的特定情况下(根据您提供的详细信息),用户可以上传/选择包含错误数据的文件。 You program should handle that by catching any basic Java runtime issues and returning information to the user (not "Exception in thread..." but something more readable to a user). 您的程序应该通过捕获任何基本的Java运行时问题并将信息返回给用户来处理(不是“线程中的异常......”,而是对用户更具可读性的东西)。 If you are checking for these alpha characters then you should just handle that (with an error to the user) without throwing an exception - unless this is truly the behavior you want. 如果你正在检查这些字母字符,那么你应该只处理它(给用户一个错误)而不抛出异常 - 除非这确实是你想要的行为。

Exception are cause when the program cannot work in a normally correct manner. 当程序无法以正常的方式工作时,会导致异常。

The exceptions get more complicated and increase in numbers when you evolve from j2se to j2ee. 当你从j2se演变为j2ee时,异常变得更复杂并且数量增加。

For a stand alone application 对于独立应用程序

  1. If your application is just a extremely simple calculator then you may just completely forget about exception because your user input would be filtered and one of the few exception would be division by zero 如果您的应用程序只是一个非常简单的计算器,那么您可能只是完全忘记了异常,因为您的用户输入将被过滤,并且少数例外之一将被除以零
  2. If your application is a simple utility tool say screen capture , then if your file cannot be saved (exception at file i/o) then all you need to do is simply terminate all your task and say some error message to the user. 如果您的应用程序是一个简单的实用工具,例如屏幕截图,那么如果您的文件无法保存(文件i / o中的例外),那么您只需终止所有任务并向用户说出一些错误消息。
  3. For an advanced project of example 2 , you need to save the image in a temp , and perform saving of file once the issue is rectified 对于示例2的高级项目,您需要将图像保存在临时文件中,并在问题得到纠正后执行文件保存

For a enterprise scaled and distributed application Here transaction(inter related activities) is involved . 对于企业扩展和分布式应用程序此处涉及事务(相互关联的活动)。 Here a simple message to the user is also needed at times and also handle(do needed changes to related transactions) the exception ! 这里有时也需要向用户发送一条简单的消息, 并处理(需要对相关事务进行更改)异常!

  1. If the application is distributed in many countries then exception in one traction needs alteration in another server in another country , this demands optional incorporation of a some thing that uses JMS API(message sending inside application) 如果应用程序在许多国家/地区分发,那么一个牵引力中的异常需要在另一个国家/地区的另一个服务器中进行更改,这需要可选地合并使用JMS API的某些内容(在应用程序内部发送消息)

  2. JPA (java persistence api) implicitly rolls back the database on event of a exception and provides facility to do so for interrelated transactions . JPA(java持久性api)在异常事件时隐式回滚数据库,并为相互关联的事务提供了这样做的工具。 But still the roll back only affects the database and not the instance variable(object values) 但仍然回滚只影响数据库而不影响实例变量(对象值)

and at all times you don't want to user to read your exact stack trace that says error at line number ..... 并且在任何时候您都不希望用户读取您在行号中显示错误的确切堆栈跟踪.....

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

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