How to treat exceptions in constructor best?

前端 未结 2 668
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 03:50

How to treat exception in best way in construct?

option1 - catch exception where object created:

class Account {
    function __cons         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-03 04:05

    What is the purpose of throwing an exception and immediately catching it? If you want to abort a function on error but not throw an error, you should return.

    Thus, your first code is always correct. Let the Exception bubble up.

提交回复
热议问题