程序错误类型
1、语法错误
- 指代码不符合C#语言的语句。Visual Studio的编译系统能查出此类错误并报告错误的原因,有语法错误的代码不能通过编译,改正后才能通过编译。
2、运行时错误
- 运行时错误相对复杂一些,指的是在程序的运行过程中产生的错误,也就是异常。
3、逻辑错误
- 逻辑错误是指程序没有实现编程人员的设计意图和功能。有这类错误的程序可以运行,但是程序运行结果与预期不同。逻辑错误一般是因算法考虑不周引起的,也有些是因为编码时疏忽。
- 逻辑错误是VS无法识别的,要解决逻辑错误,则必须通过设置断点,进行程序调试,跟踪程序的运行过程,分析错误原因,然后修改程序代码。
程序调试
程序调试的主要目的是解决程序中的逻辑错误,通过设置断点,跟踪观察程序的执行过程,发现造成逻辑错误的具体语句,然后修改程序实现设计目标。
设置断点:断点是程序暂停执行的地方,当程序运行到断点位置时,程序暂停执行,进入中断模式,以便观察程序的运行状态。
启动/F5
逐语句调试:F11
逐过程调试:F10,遇到函数,不会进入函数内部,把函数当成一条语句执行。
异常处理
异常提供了一种把程序控制权从某个部分转移到另一个部分的方式。C# 异常处理时建立在四个关键词之上的:try、catch、finally 和 throw。
- try:一个 try 块标识了一个将被激活的特定的异常的代码块。后跟一个或多个 catch 块。
- catch:程序通过异常处理程序捕获异常。catch 关键字表示异常的捕获。
- finally:finally 块用于执行给定的语句,不管异常是否被抛出都会执行。
- throw:当问题出现时,程序抛出一个异常。使用 throw 关键字来完成。
语法格式:
try
{
// 引起异常的语句
}
catch( ExceptionName e1 )
{
// 错误处理代码
}
catch( ExceptionName e2 )
{
// 错误处理代码
}
catch( ExceptionName eN )
{
// 错误处理代码
}
finally
{
// 要执行的语句
}
代码示例:
- 无异常
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_CSharp
{
class Program
{
static void Main(string[] args)
{
string s = "1";
try
{
Console.WriteLine("int:"+int.Parse(s));
}
catch (Exception e)
{
throw e;
}
finally
{
Console.WriteLine("string:"+s);
}
Console.ReadKey();
}
}
}
--->
int:1
string:1
- 异常一
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_CSharp
{
class Program
{
static void Main(string[] args)
{
string s = "a";
try
{
Console.WriteLine("int:"+int.Parse(s));
}
catch (Exception e)
{
throw e;
}
finally
{
Console.WriteLine("string:"+s);
}
Console.ReadKey();
}
}
}
--->
抛出异常
异常二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_CSharp
{
class Program
{
static void Main(string[] args)
{
string s = "a";
try
{
Console.WriteLine("int:"+int.Parse(s));
}
catch (Exception)
{
Console.WriteLine("Error");
}
finally
{
Console.WriteLine("string:"+s);
}
Console.ReadKey();
}
}
}
--->
Error
string:a
异常类 | 描述 |
---|---|
System.IO.Exception | 处理 I/O 错误。 |
System.IndexOutOfRangeException | 处理当方法指向超出范围的数组索引时生成的错误。 |
System.ArrayTypeMismatchException | 处理当数组类型不匹配时生成的错误。 |
System.NullReferenceException | 处理当依从一个空对象时生成的错误。 |
System.DivideByZeroException | 处理当除以零时生成的错误。 |
System.InvalidCastException | 处理在类型转换期间生成的错误。 |
System.OutOfMemoryException | 处理空闲内存不足生成的错误。 |
System.StackOverflowException | 处理栈溢出生成的错误。 |
自定义异常类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_Error
{
class Program
{
static void Main(string[] args)
{
try
{
string s = "aa";
if (s == "aa")
throw new TextException("转换失败!");
}
catch (TextException e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
class TextException : Exception
{
public TextException() : base() { }
public TextException(string s) : base(s) { }
public TextException(string s, Exception e) : base(s,e) { }
}
}
--->
转换失败!