• λ我爱Aspx >> Asp.Net >> Try...Catch...Finally 和Throw 叙述
  • Try...Catch...Finally 和Throw 叙述

  • :aspxer  Դ:internet  :2007-4-29 23:44:42  ؼ:
  • Sub Button1_Click(Sender As Object,e As Eventargs)

    Try

    Response.Write("自行丢出一个Exception 例外<Br>")

    Throw New Exception

    Catch ex As Exception

    Response.Write(ex.ToString())

    End Try

    End Sub

    </Script>

    </Html>

    Try....Catch 的结构化使得我们的例外处理变的相当有效率,我们可以在程序代码中使用Try....Catch 结构来处里我们的例外,不用担心程序的执行流程被改变。使用Try....Catch 要注意不破坏原有的程序代码结构,例如下列程序代码片段是错误的示范:

    Try

    For shtI=0 To 9

    Response.Write(shtI)

    Catch ex As Exception

    ...

    Next

    巢状的Try...Catch

    我们可以在Catch 区块或Finally 区块中再使用Try....Catch 叙述,如下列范例所示:

    <Html>

    <Form Runat="Server">

    <ASP:Button Id="Button1" Text="产生例外" OnClick="Button1_Click"

    Runat="Server"/>

    </Form>

    <Script Language="VB" Runat="Server">

    Sub Button1_Click(Sender As Object,e As Eventargs)

    Try

    Response.Write("自行丢出一个Exception 例外<P>")

    Throw New Exception

    Catch err As Exception

    Try

    Response.Write(err.ToString() & "<P>")

    Response.Write("再产生一个例外<P>")

    Throw New IndexOutOfRangeException()

    Catch err1 As IndexOutOfRangeException

    Response.Write(err1.ToString())

    End Try

    End Try

    End Sub

    </Script>

    </Html>

Ҷƪл˵?
  • һƪTemplateColumn
    һƪ常见的例外对象