How to throw good exceptions?

前端 未结 12 1392
情话喂你
情话喂你 2020-12-14 21:05

I heard you should never throw a string because there is a lack of information and you\'ll catch exceptions you dont expect to catch. What are good practice for throwing exc

12条回答
  •  盖世英雄少女心
    2020-12-14 22:02

    For a current project, we thought about the appropriate action that could be taken by the main program loop. The basic program accepts XML messages, and saves the information into a database (with a fair amount of processing in between).

    1. Data errors that indicate something wrong the input. Appropriate action is to save the message to a log directory but not process it.
    2. Infrastructure errors that indicate some subcomponent (like the input queue, an SQL database, a JNI library) is malfunctioning. Sleep for a few minutes then reconnect.
    3. Configuration errors that indicate some aspect configuration is unworkable. Exit the program.

    The first item is a checked exception, since we considered data checking to be part of a method's interface. The others are unchecked since the main loop cannot know the implementations of subcomponents, e.g. an implementation may use an SQL database, or may simply persist data in memory -- the caller doesn't need to know.

提交回复
热议问题