188 lines
5.3 KiB
C#
188 lines
5.3 KiB
C#
|
|
using System;
|
|||
|
|
|
|||
|
|
namespace Lesson08
|
|||
|
|
{
|
|||
|
|
class Program
|
|||
|
|
{
|
|||
|
|
static void Main(string[] args)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("=== 异常处理演示 ===\n");
|
|||
|
|
|
|||
|
|
// ========== 基本 try-catch ==========
|
|||
|
|
Console.WriteLine("--- 基本 try-catch ---");
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
int[] numbers = { 1, 2, 3 };
|
|||
|
|
Console.WriteLine(numbers[10]);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"捕获异常: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ========== 捕获特定异常 ==========
|
|||
|
|
Console.WriteLine("\n--- 捕获特定异常 ---");
|
|||
|
|
string[] inputs = { "123", "abc", "9999999999999" };
|
|||
|
|
|
|||
|
|
foreach (string input in inputs)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
int num = int.Parse(input);
|
|||
|
|
Console.WriteLine($"转换成功: {num}");
|
|||
|
|
}
|
|||
|
|
catch (FormatException)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"\"{input}\" 格式错误,不是数字");
|
|||
|
|
}
|
|||
|
|
catch (OverflowException)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"\"{input}\" 数字太大");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ========== throw 抛出异常 ==========
|
|||
|
|
Console.WriteLine("\n--- throw 抛出异常 ---");
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
SetAge(-5);
|
|||
|
|
}
|
|||
|
|
catch (ArgumentException ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"捕获到: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
SetAge(200);
|
|||
|
|
}
|
|||
|
|
catch (ArgumentException ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"捕获到: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
SetAge(25); // 正常
|
|||
|
|
SetAge(0); // 正常
|
|||
|
|
|
|||
|
|
// ========== 实际项目场景 ==========
|
|||
|
|
Console.WriteLine("\n--- 设备连接场景 ---");
|
|||
|
|
MicrophoneDevice mic = new MicrophoneDevice("USB麦克风");
|
|||
|
|
|
|||
|
|
// 场景1:设备未连接
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
mic.StartListening();
|
|||
|
|
}
|
|||
|
|
catch (DeviceNotConnectedException ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"错误: {ex.Message}");
|
|||
|
|
Console.WriteLine("建议:请检查设备连接");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 场景2:连接后正常操作
|
|||
|
|
mic.Connect();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
mic.StartListening();
|
|||
|
|
Console.WriteLine("麦克风开始监听...");
|
|||
|
|
mic.StopListening();
|
|||
|
|
Console.WriteLine("麦克风停止监听...");
|
|||
|
|
}
|
|||
|
|
catch (DeviceNotConnectedException ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"错误: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
mic.Disconnect();
|
|||
|
|
Console.WriteLine("设备已断开");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ========== finally 演示 ==========
|
|||
|
|
Console.WriteLine("\n--- finally 执行时机 ---");
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("try 块执行");
|
|||
|
|
// int x = 1 / 0; // 解开这行会跳到 catch
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("catch 块执行");
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("finally 块总是执行");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Console.WriteLine("\n=== 演示完成 ===");
|
|||
|
|
Console.ReadLine();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ========== throw 示例 ==========
|
|||
|
|
static void SetAge(int age)
|
|||
|
|
{
|
|||
|
|
if (age < 0 || age > 150)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentException($"年龄必须在 0-150 之间,当前: {age}");
|
|||
|
|
}
|
|||
|
|
Console.WriteLine($"年龄设置为: {age}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ========== 自定义异常 ==========
|
|||
|
|
class DeviceNotConnectedException : Exception
|
|||
|
|
{
|
|||
|
|
public DeviceNotConnectedException(string message) : base(message) { }
|
|||
|
|
public DeviceNotConnectedException(string message, Exception inner) : base(message, inner) { }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ========== 设备类 ==========
|
|||
|
|
class MicrophoneDevice
|
|||
|
|
{
|
|||
|
|
private string name;
|
|||
|
|
private bool isConnected;
|
|||
|
|
private bool isListening;
|
|||
|
|
|
|||
|
|
public MicrophoneDevice(string name)
|
|||
|
|
{
|
|||
|
|
this.name = name;
|
|||
|
|
this.isConnected = false;
|
|||
|
|
this.isListening = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Connect()
|
|||
|
|
{
|
|||
|
|
isConnected = true;
|
|||
|
|
Console.WriteLine($"[{name}] 设备已连接");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Disconnect()
|
|||
|
|
{
|
|||
|
|
isConnected = false;
|
|||
|
|
Console.WriteLine($"[{name}] 设备已断开");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void StartListening()
|
|||
|
|
{
|
|||
|
|
if (!isConnected)
|
|||
|
|
{
|
|||
|
|
throw new DeviceNotConnectedException($"{name} 未连接,无法开始监听");
|
|||
|
|
}
|
|||
|
|
if (isListening)
|
|||
|
|
{
|
|||
|
|
throw new InvalidOperationException($"{name} 已经在监听中");
|
|||
|
|
}
|
|||
|
|
isListening = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void StopListening()
|
|||
|
|
{
|
|||
|
|
if (!isListening)
|
|||
|
|
{
|
|||
|
|
throw new InvalidOperationException($"{name} 没有在监听");
|
|||
|
|
}
|
|||
|
|
isListening = false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|