VB. NET data center charging system --- Exception Handling

Source: Internet
Author: User
Tags finally block

Exception Handling: The English name is Exceptional Handling. At that time, when I was young, I still remember that when I learned VB6.0 together that year, I often used the ONError statement. Compared with the OnError statement in traditional VB6.0, the exception handling mechanism on the. NET platform is more flexible and convenient to use. It is a new method to replace the declining Error Code method and provides the advantage that the Error Code does not have. Exception Handling cleverly and perfectly separates the receiving and processing error codes. This function clarifies the programmer's thoughts, enhances code readability, and facilitates maintainer's reading and understanding.

The exception handling (also known as error handling) function provides a way to handle any exceptions or exceptions that occur when the program is running. Exception Handling uses the try, catch, and finally keywords to try operations that may fail, the processing fails, and the resources are cleared afterwards. Exception Handling is usually taken to prevent unknown errors. The advantage of exception handling is that you don't have to worry about all kinds of errors, which provides a very effective way to handle a certain type of errors, greatly improving programming efficiency.

I. Exception Handling Try... Catch... Finally

<Span style = "font-size: 18px;"> <span style = "font-size: 18px; "> Try 'Code Catch ': Code Finally for exception handling 'cleanup End Try </span>
In the preceding statements, the Try statement block usually stores the code that needs to be checked for possible exceptions. That is to say, if the program is correct, the Try statement block is executed, the Catch Block captures the error code. Therefore, when an exception occurs during the execution of this Code, the exception is processed in the Catch Block. Finally, the code in the Finally block will be executed no matter whether an exception is thrown. The Finaly block is used to clear any resources allocated in the try block and run any code that must be executed even in the event of an exception. The Catch and Finally statements can be used together in a Try block.

To put it simply, the Try and Finally statement blocks must be run, but the Catch statement may not be run. If the code in the Try block has no error and no exception is thrown, the code in the Catch Block does not run. Instead, it skips the Catch Block and directly runs the cleanup work in the Finally block. If an exception Catch Block is encountered, the processing is required. Why is the Finally block cleaned up? Simply put, a program exception will cause the program to fail to complete the work normally, and jump out of the program where the error occurs, and directly execute the code in the Catch statement block, this prevents the object Resources built during the program running from being released, wastes memory resources, and may also lead to messy data storage in the stack, therefore, no matter whether an exception occurs, the code in the Finally block will certainly run.
Taking the registration in the charging system of the VB. NET data center as an example, how do we apply exception handling in the code?

<Span style = "font-size: 18px;"> <span style = "font-size: 18px;"> Private Sub btnSave_Click (sender As Object, e As EventArgs) handles btnSave. click Try Dim RegisterManager As New BLL. registerManager Dim studentinfo As New Entity. studentinfo Dim cardinfo As New Entity. cardinfo Dim rechargeinfo As New Entity. rechargeinfo If (RegisterManager. query (txtStudentNo. text) Is Nothing And RegisterManager. queryCard (TxtCardNo. text) Is Nothing) Then studentinfo. cardNo = txtCardNo. text studentinfo. studentNo = txtStudentNo. text studentinfo. studentName = txtName. text studentinfo. sex = cmbSex. text studentinfo. department = txtDepartment. text studentinfo. grade = txtGrade. text studentinfo. stuclass = txtClass. text studentinfo. remark = txtRemark. text studentinfo. handler = frmLogin. handler. handler's User display layer does not have the handler text box. What should I do? The answer to how to obtain the information of the handler field in the database is above. Cardinfo. cardNo = txtCardNo. text cardinfo. balance = txtbalance. text cardinfo. status = "use" cardinfo. registerDate = CStr (Format (Now (), "yyyy-MM-dd") cardinfo. registerTime = CStr (Format (Now (), "HH: mm: ss") cardinfo. handler = frmLogin. handler. handler cardinfo. ischeck = "unpaid" rechargeinfo. cardNo = txtCardNo. text rechargeinfo. rechargeAmount = txtbalance. text rechargeinfo. rechargeDate = CStr (Format (Now (), "yyyy-MM-dd") rechargeinfo. time = CStr (Format (Now (), "HH: mm: ss") rechargeinfo. userID = frmLogin. handler. handler RegisterManager. insert (studentinfo) RegisterManager. insertCard (cardinfo) RegisterManager. insertRecharge (rechargeinfo) MsgBox ("User Registration successful") End If Catch ex As Exception MessageBox. show (ex. message. toString () End Try End Sub </span>
If we do not write the statement to capture exceptions, the registration fails at Layer D. What will happen to the running result?

As shown in, she will jump directly to a wrong place, but what would happen if she wrote Try Catch in the program?

There are two different error reporting methods. The second method is more intuitive, which helps us to clarify our thoughts, enhance code readability, and facilitate maintainer's reading and understanding.

Ii. Throw an exception

When an exception occurs in the program, the system jumps out of the program in advance and uses Catch to Catch the exception, so that we can continue to execute the Catch content. Try... catch captures errors detected by the system. If we need to customize some errors, for example, when we find that the card number does not exist or other errors, we can use the Throw keyword to Throw an exception in the program.
The Throw keyword is usually used in the method body and an exception object is thrown. If a method does not capture any possible exceptions, other methods that call this method should capture and handle exceptions. The program is terminated immediately when the throw statement is executed, and none of the subsequent statements are executed. After throw an exception, if you want to capture and handle the exception in the code above, you need to use the throws keyword in the method that throws the exception to specify the exception to be thrown in the method declaration; if you want to catch the exception thrown by throw, you must use the try-catch statement. For example, to ensure that the logic is correct, an exception can be thrown if the program judges that the logic is in violation. For example, no number can be divided by 0, if 0 is used as the divisor, an exception is thrown. throw new exception ("0 cannot be used as the divisor! ")

The Code is as follows:

<Span style = "font-size: 18px;"> Private Sub butnTest_Click (sender As Object, e As EventArgs) handles Button1.Click Try Dim g As Integer = 3 Dim h As Integer = 0 TextBox1.Text = GetTry (g, h) Catch ex As Exception MsgBox ("the divisor cannot be 0", vbOKOnly + vbInformation, "system message") Finally MsgBox ("End") End Try End Sub Private Function GetTry (ByVal a As Integer, B As Integer) as Integer If B = 0 Then Dim excOurOwnException As New ArgumentException ("Division cannot be 0") 'throws an exception Throw excOurOwnException End If End Function </span>
Exception Handling can separate the majority of normal code from a few exception handling methods. Next, the error categories are processed in a unified manner, and the user is notified of the specific error. Data center charging system, unfinished, To be continued ......

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.