Handle multiple return values with exceptions

Source: Internet
Author: User

When writing a program, we are likely to encounter this situation, that is, when writing a method, it needs to return multiple results. Take the student computer as an example. When a student is on the computer, we enter the student card number and call the "on the computer" function. The function on the computer requires multiple return values, for example: if the input card number does not exist, the input card number is on the machine, the input card number has insufficient balance, and the computer is successful, what should we do?

Previously, I handled this situation by returning an int type variable. The specific implementation process is as follows:


On-board functions:

If the card number does not exist then

Returns 0.

The elseif card number is on then.

Return 1

Elseif card No. Balance insufficient then

Returns 2

Elseif machine success then

Return 3

..................

Endif

 

Call the function on the machine and give the user a prompt based on the result. The implementation process is as follows:

 

If 0 then is returned

Prompt User: The card number does not exist

Elseif returns 1 then

Prompt User: The card number is on the machine

Elseif returns 2 then

Prompt User: insufficient card number balance

Elseif returns 3 then

Prompt User: Machine succeeded

..................

Endif

 

Although the function can be implemented in this way, it is not very good. After all, there are many returned values, and we must know what the returned values represent, which brings us a lot of trouble. Is there a better way to achieve this?

The following describes how to use exceptions to handle such situations:

 

On-board functions (some knowledge in VB. NET is used below ):

If then does not have the ID

Throw newapplicationexception ("this card has not been registered ")

End if

 

If the student is on then

Throw newapplicationexception ("this card number is on the computer ")

End if

 

If the student balance is less than the minimum set amount then

Throw newapplicationexception ("the card amount is insufficient, please recharge ")

End if

 

Function call:

Try

Call on-machine functions

Catch ex as exception

Msgbox (ex. Message) displays specific errors to users.

End try

 

The advantage of this writing is that you no longer need to judge the specific situation. All the situations are presented to the user through the error handling program, so you feel a lot more convenient.

 

PS: although the exception solves this problem, it seems a bit inappropriate. After all, it is solved by error handling. Once it has something to do with "errors", it will always make people feel uncomfortable, but I can't tell you what is wrong. I hope you can give me more advice. Of course, if you have any good implementation methods, please share them with us!

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.