- WPF教程
- WPF-主页
- WPF - 概述
- WPF - 环境设置
- WPF - 你好世界
- WPF - XAML 概述
- WPF - 元素树
- WPF - 依赖属性
- WPF - 路由事件
- WPF - 控件
- WPF - 布局
- WPF - 布局嵌套
- WPF - 输入
- WPF-命令行
- WPF - 数据绑定
- WPF - 资源
- WPF - 模板
- WPF - 样式
- WPF - 触发器
- WPF-调试
- WPF - 自定义控件
- WPF - 异常处理
- WPF - 本地化
- WPF-交互
- WPF - 2D 图形
- WPF - 3D 图形
- WPF-多媒体
- WPF 有用资源
- WPF - 快速指南
- WPF - 有用的资源
- WPF - 讨论
WPF - 异常处理
异常是在程序执行期间遇到的任何错误情况或意外Behave。引发异常的原因有很多,其中一些原因如下:
您的代码或您调用的代码(例如共享库)中的错误,
操作系统资源不可用,
公共语言运行时遇到的意外情况(例如无法验证的代码)
句法
异常能够将程序流程从一个部分转移到另一部分。在.NET框架中,异常处理有以下四个关键字:
try - 在此块中,程序识别出引发某些异常的特定条件。
catch - catch 关键字表示捕获异常。try块后跟一个或多个catch块,用于在程序中要处理问题的位置使用异常处理程序捕获异常。
finally -finally 块用于执行一组给定的语句,无论是否抛出异常。例如,如果打开一个文件,则无论是否引发异常,都必须关闭该文件。
throw - 当出现问题时程序会抛出异常。这是使用 throw 关键字完成的。
使用这四个关键字的语法如下 -
try {
///This will still trigger the exception
}
catch (ExceptionClassName e) {
// error handling code
}
catch (ExceptionClassName e) {
// error handling code
}
catch (ExceptionClassName e) {
// error handling code
}
finally {
// statements to be executed
}
在 try 块可能根据程序流的情况引发多个异常的情况下,使用多个 catch 语句。
等级制度
.NET框架中几乎所有的异常类都是直接或间接派生自Exception类。从 Exception 类派生的最重要的异常类是 -
ApplicationException 类- 它支持程序生成的异常。当开发人员想要定义异常时,应该从此类派生类。
SystemException 类- 它是所有预定义的运行时系统异常的基类。以下层次结构显示了运行时提供的标准异常。
下表列出了运行时提供的标准异常以及应创建派生类的条件。
| 异常类型 | 底座类型 | 描述 |
|---|---|---|
| 例外 | 目的 | 所有异常的基类。 |
| 系统异常 | 例外 | 所有运行时生成的错误的基类。 |
| 索引超出范围异常 | 系统异常 | 仅当数组索引不正确时才会由运行时抛出。 |
| 空引用异常 | 系统异常 | 仅当引用空对象时才会由运行时抛出。 |
| 访问冲突异常 | 系统异常 | 仅当访问无效内存时,运行时才会抛出该异常。 |
| 无效操作异常 | 系统异常 | 处于无效状态时由方法抛出。 |
| 参数异常 | 系统异常 | 所有参数异常的基类。 |
| 参数空异常 | 参数异常 | 由不允许参数为空的方法引发。 |
| 参数超出范围异常 | 参数异常 | 由验证参数是否在给定范围内的方法引发。 |
| 外部异常 | 系统异常 | 发生的异常或针对运行时之外的环境的异常的基类。 |
| SEH异常 | 外部异常 | Exception封装了Win32结构化异常处理信息。 |
例子
让我们举一个简单的例子来更好地理解这个概念。首先创建一个名为WPFExceptionHandling的新 WPF 项目。
将一个文本框从工具箱拖到设计窗口。以下 XAML 代码创建一个文本框并使用一些属性对其进行初始化。
<Window x:Class = "WPFExceptionHandling.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local = "clr-namespace:WPFExceptionHandling"
mc:Ignorable = "d"
Title = "MainWindow" Height = "350" Width = "604">
<Grid>
<TextBox x:Name = "textBox" HorizontalAlignment = "Left"
Height = "241" Margin = "70,39,0,0" TextWrapping = "Wrap"
VerticalAlignment = "Top" Width = "453"/>
</Grid>
</Window>
下面是 C# 中的文件读取和异常处理。
using System;
using System.IO;
using System.Windows;
namespace WPFExceptionHandling {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
ReadFile(0);
}
void ReadFile(int index) {
string path = @"D:\Test.txt";
StreamReader file = new StreamReader(path);
char[] buffer = new char[80];
try {
file.ReadBlock(buffer, index, buffer.Length);
string str = new string(buffer);
str.Trim();
textBox.Text = str;
}
catch (Exception e) {
MessageBox.Show("Error reading from "+ path + "\nMessage = "+ e.Message);
}
finally {
if (file != null) {
file.Close();
}
}
}
}
}
当您编译并执行上述代码时,它将产生以下窗口,其中文本框内显示文本。
当引发异常或手动抛出异常时(如以下代码所示),它将显示一个包含错误的消息框。
using System;
using System.IO;
using System.Windows;
namespace WPFExceptionHandling {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
ReadFile(0);
}
void ReadFile(int index) {
string path = @"D:\Test.txt";
StreamReader file = new StreamReader(path);
char[] buffer = new char[80];
try {
file.ReadBlock(buffer, index, buffer.Length);
string str = new string(buffer);
throw new Exception();
str.Trim();
textBox.Text = str;
}
catch (Exception e) {
MessageBox.Show("Error reading from "+ path + "\nMessage = "+ e.Message);
}
finally {
if (file != null) {
file.Close();
}
}
}
}
}
当执行上述代码时引发异常时,将显示以下消息。
我们建议您执行上述代码并尝试其功能。