In
<script runat="server" /> block or codebehind:
Private Sub Page_Load(Source As Object, E As EventArgs)
Dim _Connection As New
➥SqlConnection("Server=localhost;Database=pubs;uid=sa;pwd=;")
Dim _Command As New SqlCommand("Select * From Authors", _Connection)
Try
_Connection.Open()
DG1.DataSource = _Command.ExecuteReader(CommandBehavior.CloseConnection)
DG1.DataBind()
Catch _Error As Exception
Throw New Exception("Whoops.... You've got an error in the code!")
Finally
_Connection.Close()
End Try
End Sub
The Throw statement here creates a new exception and raises it, replacing the original
exception object.The error message that will appear on the page in this case is the one
that has been provided as an argument to the constructor of the Exception object.
The original error message can also be passed to the Throw statement in order to
show the details.You can achieve this by passing the Exception object as a second argument
to the constructor of the newly raised exception.
Catch _Error As Exception
Throw New Exception("Whoops.... You 've got an error in the code!", _Error)
Your exception message has been replaced by the original one, but your exception message
still appears.Where? Check out the Stack Trace section.
Comments
The Throw statement can be used to explicitly throw an exception or to rethrow a
caught exception.The new exception being raised is provided with a customized statement,
so as to hide error details and display a more simplified error message to the viewer.
You can also pass along the original exception object to the raised exception.This can
be useful because it allows a friendly error message to be displayed to the user, but keeps
the detailed information for debugging purposes.
No comments:
Post a Comment