In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "example analysis of custom exceptions caused by C# singleton mode". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "C# singleton pattern caused by the custom exception example analysis" bar!
Case code
For singleton mode, C# has a concise implementation, using static read-only fields.
But if an exception custom exception occurs in the singleton pattern constructor, can it be caught on the client side?
Code download: ConfigException defines the exception class for us, and TriggerException is the exception trigger class. Don't run it yet, guess what exception will be caught?
reality
In fact, our custom exception is not without Catch, this is a hidden phenomenon, mainly because it does not affect the normal operation of our program, but our painstaking definition of custom exception has not played any role.
Usually we work in the Portal of singleton mode. If an exception occurs, the exception granularity of the following log records is of little reference value to the operation and maintenance staff.
{the type initializer for "ConsoleApplication4.Singleton" throws an exception. " } [System.TypeInitializationException]: {"ConsoleApplication4.Singleton" type initializer throws an exception. " } Data: {System.Collections.ListDictionaryInternal} HelpLink: null InnerException: {"exception of ComplicatedCalculate"} Message: the type initializer of "ConsoleApplication4.Singleton" throws an exception. " Source: "ConsoleApplication4" StackTrace: "at ConsoleApplication4.Program.Main (String [] args) location E:\\ MyStudy\\ ArchitectureHOL\\ ConsoleApplication4\\ ConsoleApplication4\\ Program.cs: line number 16" TargetSite: {Void Main (System.String [])} solution
Or go back to the implementation of the classic singleton mode. The code is as follows:
Public class Singleton {public readonly static Singleton instance = null; private static object objectLock = new object (); public int Age {get;set } public static Singleton Instance {get {if (instance = = null) {lock (objectLock) {if (instance = = null) instance = new Singleton () } return instance;}} private Singleton () {TriggerException t = new TriggerException (); t.ComplicatedCalculate ();}}
In this way, we can Catch to our custom exception.
Reasons and doubts
Why can't a single instance catch a custom exception in readonly Static mode? This is also the call to the constructor Singleton to trigger the exception, which I can not explain clearly, and it is also my main purpose of writing this article. I hope someone will discuss it and give us some advice. But I think the following two points are important:
The C# static read-only field has an exception when assigned at run time as a dynamic constant. Net FrameWork treats it as a general exception and uses our custom exception as its InnerException
The C # static property is already seen as a method in the MSIL language. So the method exception it calls will be returned to the caller at the next level by Catch.
At this point, I believe that everyone has a deeper understanding of the "C# singleton pattern caused by custom exception example analysis", might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.