In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about exception handling instance analysis of classes in C#. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Preface
The most important skill in the daily coding process is not that you learn to use many of the latest programming techniques or build a high-end system. It is whether the system is stable and robust when you handle the exception in the process of writing code.
Exception handling is caught through the try/cath mechanism in C #, so let's take a look at it.
An exception is a run-time error in a program that violates system or application constraints, or occurs situations that are not expected during normal operation.
For example, some variables are assigned abnormally or there are errors when participating in the operation.
Abnormal
With regard to exceptions, the capture of exceptions is guaranteed by try statements and exception handling mechanism classes in C # to prevent exception reminders or even crashes in the whole program due to the failure to catch exceptions.
Abnormal requirements
The try block contains code that is protected to avoid exceptions
The catch clause section contains one or more catch clauses. (exception handling code snippets are also called exception handling subroutines)
The finally block contains code that will be executed in all cases, with or without an exception
Class Program {static void Main (string [] args) {try {/ / verify whether there is an exception code Ahui hui = new Ahui (); InfoBase info = (InfoBase) hui; Console.WriteLine (hui.GetInfo ()); Console.WriteLine (info.GetInfo ()) } catch (Exception ex) {/ / catches the exception and handles the exception throw new Exception ("the exception is thrown up one layer!") ;} Console.ReadKey ();}}
When dealing with business code, you must clearly consider all logical errors at the beginning for verification processing, such as null, abnormal values, value type errors, and so on.
Secondly, through the try/catch mechanism to catch abnormal exceptions, to prevent the program from uncontrollable errors, resulting in collapse.
Tip: catch exceptions as little as possible in business programs, and pre-process exceptions as soon as possible if the conditions are met.
There are many different types of exceptions that can occur in C #, and BCL defines a number of classes, each of which represents a specified exception type. When an exception occurs, CLR first creates an exception object of that type, and then looks for the appropriate catch clause to handle it. (all exception classes are derived from the System.Exception class)
There are three forms of catch () that can handle different levels of exceptions.
The first general exception handling
Try {} catch {/ / General exception handling}
Can accept any exception, but cannot confirm the type that caused the exception. You can handle and clean up possible exceptions.
The second specific exception handling
Try} catch (Exception) {/ / specific exception handling}
Takes the name of an exception class as a parameter that matches the exception of the specified class or the exception class that derives from it.
The third kind of specific exception handling with objects
Try} catch (Exception ex) {throw new Exception (ex.Message);}
Provides a variety of exception information about an exception that matches the specified exception or an exception derived from its exception class. Through the exception instance, you can get the details of the exception.
Try} catch (Exception ex) {/ / specific exception handling throw new Exception (ex.Message);} finally {/ / handling mechanism after exception occurrence}
It needs to be noted here that the code in finally is executed regardless of whether the exception is caught or not.
It is appropriate to store code that releases resources or subsequent processing in finally.
These are all the contents of the article "exception handling instance Analysis of classes in C#". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.