In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to deal with C# exceptions". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to deal with C# exceptions.
Exception is an error that occurs in the operation of a program, and C# exception handling is a part of program design. In C # exception handling is done through the Exception base class, and you can create your own exception class, but this class must inherit from the Exception base class.
Exceptions will lead to imperfect or unwanted results, so exceptions need to be handled in programming. The exception can also be an error such as "IndexOutOfBounds", which indicates that the program is trying to access an element that exists in the middle of the array; for example, the array * element is 100, which occurs when you try to access element 101. However, these are built-in exceptions and can be handled directly through the Exception base class. If the exception that occurs is not built-in, then we need to see a C# exception handling class ourselves, which inherits from the Exception base class.
The following program defines its own exception class:
Using System; public class MyException:Exception {public string s; public MyException (): base () {s=message.ToString null;} public MyException (stringmessage): base () {s=message.ToString ();} public MyException (stringmessage, Exception myNew): base (message,myNew) {s=message.ToString (); / / Stores new exception message into class member s} public static void Test () {string str,stringmessage; bool flag=false; stringmessage=null; char ch=''; int iTun0 Console.Write ("Please enter some string (less than 27 characters) -"); str=Console.ReadLine (); try {ch=str [I]; while (flag==false) {if (ch=='\ r') {flag=true;} else {ch=str [I]; iTunes;} catch (Exception e) {flag=true;} if (I > 27) {stringmessage= "you can enter no more than 27 words!"; throw new MyException (stringmessage) }} public static void Main () {try {Test ();} catch (MyException e) {Console.WriteLine (e.s);}
The above code creates a new exception class called MyException, which inherits from the Exception base class, which has three overloaded constructors for a simple purpose: to be able to overload the base class's constructor. The default and implemented constructors in the base class must be implemented in the inheritance class, but the real purpose of our design of this class is to display an error message when the user enters more than 27 words, reminding the user to enter no more than 27 words. Although this is more like data validation than a real exception, it is a good example. The new exception MyException is thrown by the function Test (), and the method message "your input can not exceed 27 words" is obtained by the catch block of main ().
Thank you for your reading, the above is the content of "how to handle C# exceptions". After the study of this article, I believe you have a deeper understanding of how to deal with C# exceptions, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.