When will throw an exception when to catch an exception?

Before the interview encountered such a problem, an exception handled in two ways, one is the direct capture try catch, is a direct throw out throws, then this is two ways to use it and under what circumstances

At that time it did not react, most of them are working in direct capture, only later we learned that first you have to figure out the function of the position:

  • If no one calls this method, be sure to capture or else thrown jvm to deal with (such as the Controller layer are generally to be abnormal capture, the fight hit the log, throwing the front desk throwing)
  • There are other callers you want to use this method if this exception information is not required to let the caller know that the direct capture, such as the conversion time, you can directly use SimpleDateFormat catch exceptions
  • But if the caller is an exception would need to know to throw to allow the caller to deal with, such as controller layer transfer service business layer, if the business logic layer processing failure is the cause of his failure to get the required operation and maintenance convenience, this situation we should throw an exception

For the service layer if the database transaction management generally have CRUD operations, this time you can not just catch an exception, otherwise it will affect the rollback of things (and then throw or catch a custom exception)

Guess you like

Origin www.cnblogs.com/chenglei0718/p/11407447.html