| |

VerySource

 Forgot password?
 Register
Search
Author: hzjbwl

try..catch problem

[Copy link]

0

Threads

27

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-3-2 05:45:01
| Show all posts
In a windows program, if an exception thrown by the program is caught by the system, it will prompt the program to error, and then the program automatically closes, so you must catch the exception yourself to prevent the program from closing automatically. This is one use of try catch, and the other is Custom error message.
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-3-2 23:15:01
| Show all posts
Let me post the important code: Microsoft demo
1. public System.Data.DataSet Retrieve (string sql)
{
SQL.SqlConnection dbConn = null;
System.Data.DataSet ds = new System.Data.DataSet () ;;

try
{
dbConn = new SQL.SqlConnection (this.sConnection);

SQL.SqlCommand dbCmd;
dbCmd = new SQL.SqlCommand (sql, dbConn);

SQL.SqlDataAdapter dbAdapter;
dbAdapter = new SQL.SqlDataAdapter (dbCmd);

dbConn.Open ();
dbAdapter.Fill (ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
try {dbConn.Close ();}
catch {}
}

return ds;
}






2. The statement captured centrally is

Application.ThreadException + = new ST.ThreadExceptionEventHandler (App.OnAppException);

// setup cleanup routine for normal app shutdown ...
Application.ThreadExit + = new System.EventHandler (App.OnAppExit);

Since the ex captured in 1 is thrown without doing anything, it will eventually be handled centrally by ThreadException in 2. If it is not captured, the ThreadException in 2 can also be handled centrally, so why add it?
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-6 11:45:02
| Show all posts
When debugging, if you don't try it yourself, it is very likely that you won't see the exception during debugging.
Another thing is that what you see is a demo in itself, which means that you must try, and the processing given is just an example. Of course, you can add other exception handling logic yourself.
It is definitely beneficial to add appropriate exception handling when writing code. It is more convenient to put the code skeleton there in advance and write the exception handling logic when appropriate.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-7 07:45:01
| Show all posts
General exception handling method:
   1. Do nothing, ignore the exception directly
   2. Handle the exception directly (for example, perform some operations, pop up a personalized dialog box, prompt the problem)
   3.Retry the operation and handle the exception when the number is reached
   4.Rethrow the exception and let the superior handle the exception
   5.Record to log for easy viewing.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-7 10:00:01
| Show all posts
It is sometimes useful for the superior to handle this exception. So rethrow it after catching the exception
Reply

Use magic Report

2

Threads

29

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-3-10 17:30:01
| Show all posts
Rethrowing can be processed by the above.If your block does not process, it does not mean that it is not processed.
If it is a more common exception or not a more detailed exception, the outer frame should handle it. This design is more reasonable
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list