课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - 异常处理

当前位置:免费教程 » 程序设计 » VB.Net
异常是在程序执行期间出现的问题。 例外是对程序运行时出现的异常情况的响应,例如尝试除以零。

异常提供了一种将控制从程序的一个部分转移到另一个部分的方法。 VB.Net异常处理建立在四个关键字:Try,Catch,Finally和Throw。

  • Try: A Try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more Catch blocks.  Try块标识将激活特定异常的代码块。 它后面是一个或多个Catch块。

  • Catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The Catch keyword indicates the catching of an exception.  程序捕获异常,并在程序中要处理问题的位置使用异常处理程序。 Catch关键字表示捕获异常。

  • Finally: The Finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.  最后:Finally块用于执行给定的一组语句,无论是抛出还是不抛出异常。 例如,如果打开一个文件,那么无论是否引发异常,都必须关闭该文件。

  • Throw: A program throws an exception when a problem shows up. This is done using a Throw keyword.  当出现问题时,程序抛出异常。 这是使用Throw关键字完成的。

语法

假设块将引发异常,则方法使用Try和Catch关键字的组合捕获异常。 Try / Catch块放置在可能生成异常的代码周围。 Try / Catch块中的代码称为受保护代码,使用Try / Catch的语法如下所示:

  1. Try
  2. [ tryStatements ]
  3. [ Exit Try ]
  4. [ Catch [ exception [ As type ] ] [ When expression ]
  5. [ catchStatements ]
  6. [ Exit Try ] ]
  7. [ Catch ... ]
  8. [ Finally
  9. [ finallyStatements ] ]
  10. End Try

您可以列出多个catch语句以捕获不同类型的异常,以防您的try块在不同情况下引发多个异常。


.Net框架中的异常类

在.Net框架中,异常由类表示。 .Net Framework中的异常类主要直接或间接从System.Exception类派生。 从System.Exception类派生的一些异常类是System.ApplicationException和System.SystemException类。

System.ApplicationException类支持由应用程序生成的异常。 所以程序员定义的异常应该从这个类派生。

System.SystemException类是所有预定义系统异常的基类。

下表提供了从Sytem.SystemException类派生的一些预定义异常类:

异常类描述
System.IO.IOExceptionHandles I/O errors.
处理I / O错误。
System.IndexOutOfRangeExceptionHandles errors generated when a method refers to an array index out of range.
当处理的方法是指一个数组索引超出范围产生的错误。
System.ArrayTypeMismatchException

Handles errors generated when type is mismatched with the array type

处理类型与数组类型不匹配时生成的错误。.

System.NullReferenceExceptionHandles errors generated from deferencing a null object.
处理从取消引用空对象生成的错误。
System.DivideByZeroExceptionHandles errors generated from dividing a dividend with zero.
处理将股利除以零所产生的错误。
System.InvalidCastExceptionHandles errors generated during typecasting.
处理类型转换期间生成的错误。
为System.OutOfMemoryExceptionHandles errors generated from insufficient free memory.
处理来自可用内存不足产生的错误。
System.StackOverflowExceptionHandles errors generated from stack overflow.
处理来自堆栈溢出产生的错误。


处理异常

VB.Net提供了一个结构化的解决方案,以try和catch块的形式处理异常处理问题。 使用这些块,核心程序语句与错误处理语句分离。

这些错误处理块使用Try,Catch和Finally关键字实现。 以下是在零除条件时抛出异常的示例:

  1. Module exceptionProg
  2. Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
  3. Dim result As Integer
  4. Try
  5. result = num1 num2
  6. Catch e As DivideByZeroException
  7. Console.WriteLine("Exception caught: {0}", e)
  8. Finally
  9. Console.WriteLine("Result: {0}", result)
  10. End Try
  11. End Sub
  12. Sub Main()
  13. division(25, 0)
  14. Console.ReadKey()
  15. End Sub
  16. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. Exception caught: System.DivideByZeroException: Attempted to divide by zero.
  2. at ...
  3. Result: 0


创建用户定义的异常

您还可以定义自己的异常。 用户定义的异常类派生自ApplicationException类。 以下示例演示了这一点:

  1. Module exceptionProg
  2. Public Class TempIsZeroException : Inherits ApplicationException
  3. Public Sub New(ByVal message As String)
  4. MyBase.New(message)
  5. End Sub
  6. End Class
  7. Public Class Temperature
  8. Dim temperature As Integer = 0
  9. Sub showTemp()
  10. If (temperature = 0) Then
  11. Throw (New TempIsZeroException("Zero Temperature found"))
  12. Else
  13. Console.WriteLine("Temperature: {0}", temperature)
  14. End If
  15. End Sub
  16. End Class
  17. Sub Main()
  18. Dim temp As Temperature = New Temperature()
  19. Try
  20. temp.showTemp()
  21. Catch e As TempIsZeroException
  22. Console.WriteLine("TempIsZeroException: {0}", e.Message)
  23. End Try
  24. Console.ReadKey()
  25. End Sub
  26. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. TempIsZeroException: Zero Temperature found


掷物投掷对象

如果它是直接或间接从System.Exception类派生的,你可以抛出一个对象。

你可以在catch块中使用throw语句来抛出当前对象:

  1. Throw [ expression ]


下面的程序说明了这一点:

  1. Module exceptionProg
  2. Sub Main()
  3. Try
  4. Throw New ApplicationException("A custom exception _
  5. is being thrown here...")
  6. Catch e As Exception
  7. Console.WriteLine(e.Message)
  8. Finally
  9. Console.WriteLine("Now inside the Finally Block")
  10. End Try
  11. Console.ReadKey()
  12. End Sub
  13. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. A custom exception is being thrown here...
  2. Now inside the Finally Block
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号