Reconstructing the charge system of the computer room you're going to use--exception handling and throwing exceptions (try Catch finally)--(vb.net)

Source: Internet
Author: User
Tags try catch

Can you guarantee that your program will not be a problem?

No

When your program runs to a place where you don't want the results, do you want to make it wrong again?

Don't want to

Do you want your program to take up manger and not poop?

Don't want to

Do you want to know the reason why your program is wrong?

Think

After looking at these 4 questions, if your answer is consistent with mine, see below

Use of the Try Catch finally:

Try ' The program you want to write catch ' captures all occurrences of the exception in the program you want to write finally ' no matter what happens or does not happen, you want to execute the code

Example:
Try            Dim update_info As String = "Update" & Table & "Set" & fieldandcontent    ' modified SQL statement            Dim Dbconne Ctstr as new connectstringdal    ' database connection string            Dim DBConnection as New SqlConnection (dbconnectstr.connectstring)   ' Database Connection object            Dbconnection.open () ' Connect database            Dim dbcmd as New SqlCommand (Update_info, DBConnection)  ' Execute Modify statement            Result = ' Successfully modified: "& Dbcmd.executenonquery &" Row "        Catch ex as Exception            result =" Error occurred during data modification: "& ex. Message
    MsgBox Result
Finally
Dbconnection.close        End Try

If your program fails to connect to the database, and if there is no hint that you can refer to, then you may think the query is wrong, not the SA user's login password. And as on the procedure, if
Dbconnection.open () ' Connect to Database
It went wrong, then she jumps straight to catch ex As exception and executes it the following statement, by calling Ex.message, it returns the cause of the error, possibly the user name or password is wrong. You can then write down the code that closes the database connection at finally, which prevents the program from encountering an error directly jumping out of the program without releasing the resources (which is tantamount to manger).

Back to the second question, when the program happened you do not want the result, he will continue to run, just said encountered an error it will jump out, but do not want the result as originally this variable a should be assigned to 3, but it is 4, and this time it will not jump directly, because this is not an unusual error, May be a logical error, according to layered thinking, if it occurs in the UI layer, we in order to let the UI has a single responsibility, do not make unnecessary judgment statements, only responsible for user operation response, user data transfer and various display functions, logically do not do too much judgment and processing, At this point you can use throw exception to make your program exception, jump directly to catch there. Like the code above.
Try            Dim update_info As String = "Update" & Table & "Set" & fieldandcontent    ' modified SQL statement            Dim Dbconne Ctstr as new connectstringdal    ' database connection string            Dim DBConnection as New SqlConnection (dbconnectstr.connectstring)   ' Database Connection object
    Throw New argumentoutofrangeexception' throws an exception
            Dbconnection.open () ' Connect database            Dim dbcmd as New SqlCommand (Update_info, DBConnection)  ' Execute Modify statement            Result = "Successfully modified: "& Dbcmd.executenonquery &" Row "        Catch ex as Exception            result =" Error occurred during data modification: "& ex. Message
    MsgBox Result
Finally
Dbconnection.close        End Try
throw an exception before connecting to the database, note that this exception is made by you artificially, it is also a type of exception, so this time it does not execute the connection database and the following code until the catch, he will execute catch the following code, and display the reason for the exception. It is worth noting that finally the statement, regardless of whether the program will be an exception, it will certainly be executed, it is common to measure the implementation of what the program is good, will generally write some code to release resources!
What the hell did I do wrong? Since the code text has been written, the following words are all code format!!! Csdn ...

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.