using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace TryCatchTest_WindowsFormsApp
{
public partial class Form1 : Form
{
static int Calc(int x) => 10 / x;
public Form1()
{
InitializeComponent();
int y = Calc(0);
Debug.WriteLine(y);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
运行以上程序,弹出:
加上try catch 之后代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Ta