Skip to content

Instantly share code, notes, and snippets.

@icecoobe
Created February 28, 2017 06:18
Show Gist options
  • Save icecoobe/7da6d43bd2c4a4f726d102d293119262 to your computer and use it in GitHub Desktop.
Save icecoobe/7da6d43bd2c4a4f726d102d293119262 to your computer and use it in GitHub Desktop.
Exception handling of multiple-threaded model in C#
using System;
using System.Threading;
using System.Windows.Forms;
namespace ET_WinForm
{
public partial class ExceptionTestForm : Form
{
Thread worker = new Thread(Go);
Thread worker2 = new Thread(Back);
public ExceptionTestForm()
{
InitializeComponent();
}
public static void Go()
{
//throw new Exception("Hello");
int c = 0;
int i = 5 / c;
}
public static void Back()
{
try
{
int c = 0;
int i = 5 / c;
}
catch (Exception ex)
{
MessageBox.Show("[Inner]" + ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
// 不会捕捉Go内部的异常的
try
{
if (!worker.IsAlive)
{
worker.Start();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (!worker2.IsAlive)
{
worker2.Start();
}
}
catch (Exception ex)
{
MessageBox.Show("[Outter] "+ex.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment