Try-catch-finally: How should I use the reverse?

Source: Internet
Author: User

Today, a netizen asked me a question.
She said that she had a page where she saved data to the database. If the data was not saved successfully, no error was reported. She did not know what the problem was.
I asked her if she had any exception information during the operation. She said no. I immediately felt a serious challenge from her which was a bit unreasonable.

Ask her to askCodeI was speechless. The Code is as follows:

  1   Public     Partial     Class  _ Default: system. Web. UI. Page
2 {
3 Private String Strcon = Configurationmanager. connectionstrings [ " Constr " ]. Tostring ();
4
5 Protected Void Page_load ( Object Sender, eventargs e ){}
6
7 Protected Void Login_click ( Object Sender, eventargs E)
8 {
9 Oledbconnection con = New Oledbconnection (strcon );
10 String SQL = " Insert into text (uname, unickname) values (@ uname, @ unickname) " ;
11 Oledbcommand cmd = New Oledbcommand (SQL, con );
12 Try
13 {
14 Cmd. Parameters. addwithvalue ( " @ Uname " , This . Tbuname. Text );
15 Cmd. Parameters. addwithvalue ( " @ Unickname " , This . Tbunickname. Text );
16 Con. open ();
17 Int I = Cmd. executenonquery ();
18 If (I > 0 )
19 {
20 Response. Write ( " Registered successfully " );
21 }
22 Else
23 {
24 Response. Write ( " Registration failed " );
25 }
26 }
27 Catch (Exception ex)
28 {}
29 Finally
30 {
31 Con. Close ();
32 }
33 }
34 }
35  

From the code above, it is not difficult to see why she did not save successfully or report an error.
She used a try {} catch () {} finally {} statement group to capture exceptions in database operations. However, after the capture, no processing was done and the process was completed. If the file is not saved successfully, an exception will be thrown theoretically. However, if the exception is caught, no corresponding processing is performed. Of course, the error message cannot be displayed.

 

As we all know, try {} catch () {} finally {} is an exception handling mechanism, which provides a custom mechanism for handling errors, separate the error identification code from the error handling code. We can use it to process applications.ProgramCan be predicted.
When we use it, it indicates that we must take the initiative to handle exceptions for the specified code segment. That is to say, the catch statement will capture the specified type of errors in the try statement block, then, use the catch statement block to perform corresponding processing operations.
If the catch code block does not contain any processing statement, it means we have to discard the exception, that is, ignore the exception.

 

Here I want to say that exception handling is a technology that must be used selectively and never be abused.
Exception Handling is performed to enhance Application robustness, rather than exception handling is required in every code segment that may generate exceptions. In particular, use empty catch statement blocks with caution. unless you really want to discard this exception information, you do not even want to record the logs.

 

Here, I suggest that beginners do not add the exception capture function when debugging this program. After writing the code, select the statement segment that determines the exception information to handle the exception.

 

Be cautious with the inexplicable exception capture code.

 

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.