简体   繁体   中英

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

I have my program working and all done (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.

  • 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). 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.

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.
  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

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)

  2. JPA (java persistence api) implicitly rolls back the database on event of a exception and provides facility to do so for interrelated transactions . 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 .....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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