当前位置:网站首页>Exception handling of classes in C #
Exception handling of classes in C #
2022-07-01 09:13:00 【Andy ahui】
Welcome to be my reader , I hope this article can give you some help .
Preface
During the daily coding process , The most important skill is not that you learn to use many of the latest programming techniques or make a tall system . It's when you're writing code , Handling of exceptions , Whether the system can be stable , robust .
For exception handling in C# China is through try/cath Mechanism to capture , Let's take a look at .
An exception is a runtime error in a program , It violates system constraints or application constraints , Or something unexpected in normal operation .
For example, abnormal assignment of some variables or errors during operation .
abnormal
About anomalies , stay C# Pass through try Statement and exception handling mechanism class ensure exception capture , Prevent the whole program from being reminded of exceptions due to the non capture of exceptions , Even collapse .
Unusual requirements
- try Blocks contain code that is protected from exceptions
- catch The clause section contains one or more catch Clause .( The exception handling code segment is also called exception handling subroutine )
- finally Blocks contain code to be executed in all cases , Whether there is any abnormality or not
class Program
{
static void Main(string[] args)
{
try
{
// Verify whether there is abnormal code
Ahui hui = new Ahui();
InfoBase info = (InfoBase)hui;
Console.WriteLine(hui.GetInfo());
Console.WriteLine(info.GetInfo());
}
catch (Exception ex)
{
// Capture exception , Handle exceptions
throw new Exception(" An exception is thrown up one level !");
}
Console.ReadKey();
}
}
When processing business code , Logical errors must be considered clearly at the beginning for verification , For example, judge empty , Abnormal value , Wrong value type, etc .
Secondly, through try/catch Mechanism to catch abnormal exceptions , Prevent the program from making uncontrollable errors , Leading to a rout .
Tips : Use exceptions as little as possible to catch in business programs , If the conditions are met, the exception should be handled in advance as soon as possible .<Catch() The exception handling time in will be relatively long , And it will lead to the waste of system resources ( Exceptions are thrown up in real time ).>
stay C# There are many different types of exceptions that can occur in ,BCL Many classes are defined , Each class represents a specified exception type . When an exception occurs ,CLR First create an exception object of this type , Second, find the right catch Clause to handle it .( All exception classes are derived from System.Exception class )
About catch() There are three forms , Can handle different levels of exceptions .
The first one is General exception handling
try
{
}
catch
{
// General exception handling
}
Can accept any exception , But we can't confirm the type of exception . The possible exceptions can be handled and cleaned up .
The second kind Specific exception handling
try
}
catch(Exception)
{
// Specific exception handling
}
Take the name of an exception class as a parameter , An exception that matches the specified class or an exception class derived from it .
The third kind of Specific exception handling with object
try
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
A variety of exception information about exceptions can be provided , An exception that matches the specified exception or an exception class derived from it . 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 {
// The handling mechanism after an exception occurs
}
Here's a special explanation , About finally The code in , Whether the exception is caught or not , It will be executed .
stay finally Code suitable for releasing resources or subsequent processing .
Basic knowledge day has been 29 God , If you want to continue , You can pay attention to , Thank you for your support .
remarks
Life is short , I don't want to pursue what I can't see , I just want to catch what I can see .
Originality is not easy. , Pay attention .
I am a A Hui , Thank you for reading , If it helps you , Please like it 、 forward thank you .
I'm glad to be friends with you .
边栏推荐
- Redis source code learning (29), compressed list learning, ziplist C (II)
- Log4j log framework
- How to manage fixed assets well? Easy to point and move to provide intelligent solutions
- I use flask to write the website "one"
- Tree structure --- binary tree 1
- How to realize the usage of connecting multiple databases in idel
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
- Meituan machine test in 2022
- Shell脚本-read命令:读取从键盘输入的数据
- 手指点击屏幕就模拟进入F11进入全屏
猜你喜欢

I use flask to write the website "one"

Daily practice of C language - day 80: currency change

How to launch circle of friends marketing and wechat group activities

【pytorch】softmax函数

队列的实现和应用

nacos簡易實現負載均衡

Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched

Mysql 优化
![[video game training] real topic of 2013 video game of infrared optical communication device](/img/ef/c2c45c1c6c24aed0a4e93101047372.png)
[video game training] real topic of 2013 video game of infrared optical communication device

Which method is good for the management of fixed assets of small and medium-sized enterprises?
随机推荐
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
jeecg 重启报40001
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
【pytorch】softmax函数
[video game training] real topic of 2013 video game of infrared optical communication device
Class loading
树结构---二叉树2非递归遍历
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
Daily office consumables management solution
Shell script - string
Shell script -for loop and for int loop
Pain points and solutions of equipment management in large factories
Phishing identification app
Shell脚本-read命令:读取从键盘输入的数据
Promise异步编程
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
Summary of reptile knowledge points
Shell script -select in loop
Shell script case in and regular expressions
Shell script - special variables: shell $, $*, [email protected], $$$