This is how I use exceptions (decoupling)-You don't know the exceptions (II)

Source: Internet
Author: User

Zheng quan complained to me that I only talked about text exceptions in my previous article, and I have no examples at all. I will lose my trust in this article. I will not compare the time consumed by exceptions, but I will sell you another token here.

Next I will start to give you a demo to illustrate my usage of exceptions.

The first thing I think is the example of content coupling.CodeI try to make it as simple as possible (because it is for everyone to understand, so I will mark what I want to do in Chinese, everyone understands, haha.

Code of the business logic layer:

 
Public Function login () as string if user does not exist then return "user does not exist" end if password is incorrect then return "password is incorrect" end if login is successful then return "Login successful" end if end Function

Code on the UI Layer

 
Private sub button#click (byval sender as system. object, byval e as system. eventargs) handles btnlogin. click Select case BLL. login () Case "user does not exist" execution a plan () Case "Incorrect password" execution B plan () Case "Login successful" execution C Plan () case else ticket () end select end sub

In this way, the UI Layer has to know exactly what strings are returned by the BLL layer. Of course, this actually cannot be said. Note clearly in the document. However, the coupling between the UI Layer and the BLL layer is not much to be said.

Let's take a look at the code below:

Bll Layer Code:

 
Public Function login () as Boolean if the user does not exist then throw new exception ("user does not exist") end if the password is incorrect then throw new exception ("Incorrect password ") end if login is successful then throw new exception ("Login successful") end if end Function

Code on the UI Layer:

Private sub button#click (byval sender as system. object, byval e as system. eventargs) handles btnlogin. click try if BLL. login () then login successful () end if catch ex as exception msgbox (ex. message) Exit sub end try end sub

In this way, The bll Layer Code is called at the UI Layer. The above is my use of exceptions. Of course, exceptions are useful for you to find out.
Let me talk about the use of exceptions. In the next article, we will continue to discuss how long it will take for us to handle exceptions?

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.