When do I need to resolve an application crash due to an exception in Java (design issue)?

In most cases, you can eliminate Java exceptions, even uncontrolled ones. But it is not necessary to do something (for example, from memory).

In other cases, the problem I'm trying to solve is one of the design principles. I am trying to set up a design principle or a set of rules that indicate when to throw an exception, even if it is detected on time. The goal is not to minimize the application as much as possible.

Has someone already brainstormed and talked about this? I am looking for specific general cases and possible solutions, or thumb rules.

UPDATE

Suggestions so far:

  • Stop work if data consistency could be compromised
  • Stop work if data can be deleted.
  • Stop working if you can’t do anything (Out of memory ...)
  • Stop work if the key service is unavailable or unavailable and cannot be restarted

  • The method / service should check if it can do its duty from a stable state, if it should not inform the user (log) and do nothing

  • If the application needs to be stopped, degrade as gracefully as possible
  • Use rollbacks in db transaction
  • Individual exceptions can be used to give advice on how to solve the problem by the handler.
  • Register as much relevant information as possible.
  • Tell developers
  • Maintain data state and consistency as much as possible

  • Quick fixes can be harmful, during debugging it is better to allow the application to crash and analyze in detail what caused it.

+6
source share
3 answers

The creators of Java and .Net decided to use the TYPE of the created exception object to determine when it should be caught and when it should be considered allowed, a constructive solution, probably motivated by the fact that C ++ handles exceptions. Although C ++ exception handling has been improved in many ways from what it was before, its use of the type of an exception object as a primary means of determining which exceptions were caught is not one of its best features.

Since the catching exception is controlled by the type of exception, there is no standard means for the exception to indicate whether the failed operation changed the state of any objects in the system, or whether the failure was caused by the fact that the objects are in a damaged state (unlike the state, which, although it may be unexpected caller and incompatible with the passed parameters, otherwise it would be considered that the called method is completely legal). In many cases, if, in response to a user’s request, the system calls a method that tries to do something and cannot, but this method does not adversely affect the state of any object, and the problem is not caused by the object, the damaged state is often better simply inform the user that the requested action cannot be completed and continued. Unfortunately, there is no way to distinguish β€œharmless” exceptions from the above types from those that indicate serious system-wide problems. Although 99% of exceptions are likely to be relatively harmless, a small portion will be caused by conditions that could lead to corruption of documents. If the open document was damaged, it might be better if the program crashed instantly rather than replacing a good copy on the damaged disk.

If you throw custom exceptions, you can include properties in the type of exception, which will allow the code to make it a little more useful to decide what to do with the exception. Unfortunately, many exceptions that can be thrown, harmless or not, will not include such properties.

+2
source

Why and when so that in the event of an application failure there are no specific rules ... I allow my application to crash for the following reasons:

1. Quick fixes can be discarded and potentially dangerous. I do NOT consider it correct to correct the error without knowing what is the cause of my code failure. Preventing application crashes leads me to an error in my code.

2. Preventing code crashes helps me better understand programming language and logic errors.

This is my reason for application failure.

+1
source

An application crash is something that depends on the critical level of the application and the deployment architecture.

For example, an application should not crash if it controls a rocket from the ground (with the exception of uncontrolled situations and data priority).

When applications are developed, you must design them so that the data in the data warehouses are not deleted or changed.

Conclusion There is no hard and fast way to formulate rules when applications should crash.

+1
source

Source: https://habr.com/ru/post/919604/


All Articles