Rethink about exceptions

Source: Internet
Author: User

 

He discussed exceptions with JGTM 2004 [MVP] Last time and said, "Use logical flow instead of exception capture as much as possible". I also agree

However, consider the following scenarios:

Transfer (Account from, Account to, int amount );

The above function is used to transfer funds between two accounts. If I find that the from account has no money, what should I do?

I found that the from account and to account are the same account. What should I do? In similar cases, do I use the return value to tell the caller? Obviously not suitable. In this way, it is back to the old path of structured programming. The caller has to make n + judgments ......

 

In Windows, Jeffery Richter tells us the answer: "throw an exception". Is that true?

According to his explanation, exceptions are a kind of violation of program interface assumptions.

 

Speaking of Jeffery Richter, I am also a little depressed, in his classic book (Applied Microsoft. NET Framwork) mentioned in the Exception chapter: In the design class library, we must strictly abide by the n-plus Exception handling principle he mentioned, but also said that application developers can completely deviate from these principles, to set your own policies. This is quite true. However, for example, if we want to develop both the data access layer and the business logic layer, there may also be the business appearance layer, which are class libraries and applications?

Which of the following principles should be used? The so-called application development and class library development are just relative concepts. Is it relative, but is my data access layer and business logic layer developed based on class libraries or applications ?!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.