C# try catch块内为空程序不会崩溃

catch块内为空程序不会崩溃, 只是代表它没有进行任何的处理而已。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Windows.Forms;

namespace ConsoleApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
int a = 0;
int b = 1;

int c = b / a;
}
catch
{
}

}
}
}

上面这样程序不会蹦掉,只是没有处理异常而已。

很多时候,我们将错误信息Show出来。如下:

1
2
3
4
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

当然,也可以进行其他的处理。

感谢支持!