Delegate to avoid repeated try. catch essays [reprinted], try. catch Essays
I read a blog on the use of the delegate to avoid repeated try. catch essay (original address: http://www.cnblogs.com/foolishfox/archive/2010/07/30/1788416.html), a bit of feeling, write down to share with you.
Here is an example of the original article:
public class CatchExceptions{ /// <summary> /// Methods for capturing exceptions /// </summary> /// <Param name = "func"> bind the delegate method </param> /// <Returns> exception information. If it is null, no exception is generated. </returns> public static string TryCatch(Action func) { string runMsg = string.Empty; try { func(); } catch (Exception ex) { runMsg = ex.Message; } return runMsg; } /// <summary> /// Methods for capturing exceptions /// </summary> /// <Typeparam name = "T"> delegate parameter type </typeparam> /// <Param name = "func"> bind the delegate method </param> /// <Param name = "param"> delegate parameter instance </param> /// <Returns> exception information. If it is null, no exception is generated. </returns> public static string TryCatch<T>(Action<T> func, T param) { string runMsg = string.Empty; try { func(param); } catch (Exception ex) { runMsg = ex.Message; } return runMsg; } /// <summary> /// Methods for capturing exceptions /// </summary> /// <Typeparam name = "T1"> the first parameter type of the delegate </typeparam> /// <Typeparam name = "T2"> the second parameter type of the delegate </typeparam> /// <Param name = "func"> bind the delegate method </param> /// <Param name = "param1"> the first parameter instance of the delegate </param> /// <Param name = "param2"> the second parameter instance of the delegate </param> /// <returns></returns> public static string TryCatch<T1, T2>(Action<T1, T2> func, T1 param1, T2 param2) { string runMsg = string.Empty; try { func(param1, param2); } catch (Exception ex) { runMsg = ex.Message; } return runMsg; }} |
In the original article, Func is used, and Action is used here. What is the difference between Func and Action?
The Action delegate encapsulates a method. This method does not have parameters and does not return values.
Action <T> delegate is the generic Delegate of Action to encapsulate a method. This method has a parameter and does not return a value.
Action <T> the multi-parameter form of the delegate is Action <T1, T2, T3....> and a maximum of 16 parameters can be entered.
Let's take a look at Func delegation.
Func <TResult> delegate indicates encapsulating a method that has no parameters but returns a value of the TResult type.
Func <T, TResult> delegate indicates encapsulating a method that contains a parameter but returns a value of the TResult type.
The multi-parameter form of Func <T, TResult> is Func <T1, T2, T3...., TResult> and can have up to 16 Parameters
Therefore, whether the above TryCatch method parameter uses Action or Func depends on the actual usage. If the method to be bound to the delegate has a return value, use Func. If no return value exists, use Action.
The method is defined. What if it is called?
Here we define a method with parameters and a method without parameters.
private void DeleteObj(int objID){ // The subject is omitted...} |
private void AddObj(){// The subject is omitted...} |
Call
protected void btnSave_Click(object sender, EventArgs e){ string runMsg = CatchExceptions.TryCatch(AddObj); Response.Write(runMsg.Length > 0 ? "Adding failed! " + runMsg : "Added successfully");} |
protected void btnDelete_Click(object sender, EventArgs e){ string runMsg = CatchExceptions.TryCatch(DeleteObj,objID); Response.Write(runMsg.Length > 0 ? "Deletion failed! " + runMsg : "Deleted successfully");} |
An article with 600 words should not be repeated. you can hurry up.
I stood at the top and looked at it. Everything was my scenery.
But who is my landscape? What kind of attitude does it appear in his eyes?
High school is a cruel landscape, and we have spent ten years of youth to change that sigh
I am also happy and sad, but it is no longer a pain point when it comes. In the face of those that cannot be changed, there is more fear.
Waiting is a long and suspicious process, quietly cracking my wings
The heart beats slowly, and strives to accumulate strength to bear the coming ending
More and more people say they are no longer children and they have grown up.
Maybe you have a pain or even sweat, But, dear, how can you treat you so gently?
In the end, it is just a little childish with a stubborn willfulness, a fragile and powerful flower that blooms in the decadent and vigorous passion.
Stretching the petals of each of her styles, joy and fear.
I know that one day, I will tear down all my kindness and abandon all the good and human nature.
In this age of material desires, if you want to survive, you can only get stuck in it.
It is always said that there is a reward for the deputy, but now the water can still hide my eyes and let me believe again!
Don't say that I am too pessimistic. You can't believe it. Why bother asking me to dream together ?!
I know, I'm just a normal child. I can't afford to ask too much.
I know, I'm just a dreamer. Don't wake up too early and see the cruelty of society.
Hair is getting longer and longer, and many sensitive and fragile hairs are rooted in it. In this way, it becomes a girl flying in the wind.
I am not afraid of the hardships of the next journey, because life always requires some excitement to tell myself that I am still alive!
I am just a lazy, casual girl. I never know what I want?
Do not love honor and wealth, do not love money and power,
I always do what I do. I want to make myself happy and have no intention of fighting for anything with this world.
I am the scum of the world. All the cracks in the scenery are everywhere. I am cruel to expose the ugliness and do not make any hypocrisy with you. Haha
I am like a cloud, poor white, free float, no destination, no fate
So simple, a burst of wind will be happy, a ray of sunshine will shine
Always put all the expressions on your face to keep the first appearance
I am like rain, unwilling to fall into the river, drifting, hiding in the sea of people
I want to hit my own water flower in the strongest way
In the most pure and beautiful places that need me most, dance without hesitation
I am like a fog, Misty wet, quiet and confused
No one can walk into my heart. You need to know that I am poisonous.
I live in the light of the morning, all the beautiful with phantom colors, but I will never accept the bright sunshine
400 words at random. Do not repeat
Every time you read and write, it is a kind of exquisite and calm. Today, everything is spinning in a fast pace, and everywhere is colorful and bright, which leads to the publicity and impulse of People's personality. There are temptations, competitions, and unnecessary consumption of life, every time I see the busy figure, the neon lights of wuguang, as if the whole world is a roller coaster, hovering there, and looking back at whether there is a holy land filled with cool, filled with quiet and picturesque, It is the essence of the landscape, the natural spreading, deep into the elegant feelings in the bones, it is hidden in the hearts of everyone, maybe the pressure of life, the fierce competition makes us always put it aside for the moment, and we can't find time to pick up the long-time elegance. But it is indeed rooted in our hearts and requires a kind of lonely tolerance, the spirit of the sublimation to recall that exquisite and calm. That Holy Land is in your heart.
See zhidao.baidu.com/question/81837616.html.
The following are online
I
Gentle Breeze, isolated island ......
Standing on the isolated island, I looked at the vast sea of people, busy and busy. I could not have been quiet for a moment, and I could hear the buzz every second. It seemed annoying, but I want to be integrated into them and become one of them. The fate of an isolated island is destined for the loneliness, loneliness, and loneliness of my life ......
I am eager to get busy, so I escaped from the isolated island. Even if I lost my eyes at the cost, I still walked forward without looking back. I naively thought that as long as I escaped from the isolated island, then I will be able to get out of my loneliness and live the colorful days that I have long been longing ......
But the reality is always cruel ......
I walked into the crowd and kept my eyes open to the boss. I wanted to see the green stuff, including people. I wanted to talk to those people more, but as long as I approach them, they will escape far away, just like I am on an isolated island.
I didn't care, because there are too many things that make me curious, slowly and curious. I found that loneliness hit me again, so far, I haven't really talked to that so-called person, but others keep talking about me, saying that I am crazy, and so on.
Although on the surface I don't care, but the lonely heart for a touch of sorrow ...... Finally, I escaped to an isolated island and lost my eyes ......
The wind is still gentle, and the island is still quiet. I feel the island with my heart, and loneliness becomes a wonderful music ......
Lonely, lonely, lonely ...... As I whispered, the so-called eye drops a drop of salty water drops ......
II
The stars never give up the brilliant starlight. What the mountains never give up is evergreen. What the road never gives up is that people can set foot on forever... however, what I never give up is my learning.
As time passes, I learned to care for people around me, understand others, and learn what promises are.
Looking back at the second day, I suddenly realized that I was not diligent. Although I was often praised by my teachers in the classroom, I did not feel a little happy in my life. what the teacher sees is my hypocrisy. The most sad thing is that they never find that I will be as playful as a poor student, and I do not like to learn. I always look at the training of my junior students with a smiling smile during morning exercises every day. I always feel that the training is not terrible.
On this day, I really realized the hard work of junior high school students. "You are not training for me, so do not think about training for me." Thank you! The smiling face tiger finally came out. maybe it's an environmental impact. I suddenly felt a little nervous about learning. "Spring is coming, will it be far away in winter? "But I will say," Hell is coming, Will heaven be? "Isn't the third day a hell? I know when and when I can go to that heaven.
I am unwilling to easily decide that the study in the third day is "hard" or "comfortable", but whatever the case, I have to endure it, I ...... the remaining full text>