建立自定義異常並丟擲

你可以建立自定義異常並在執行函式期間丟擲它們。作為一般做法,只有在函式無法實現其定義的功能時才應丟擲異常。

Private Function OpenDatabase(Byval Server as String, Byval User as String, Byval Pwd as String)
    if Server.trim="" then 
        Throw new Exception("Server Name cannot be blank")
    elseif User.trim ="" then 
        Throw new Exception("User name cannot be blank")
    elseif Pwd.trim="" then 
        Throw new Exception("Password cannot be blank")
    endif

    'Here add codes for connecting to the server
End function