Error Handling in ASP.NET

* I have used VB in this demonstration.
If you want to handle the error for the whole application in ASP.NET, you could use the global.asax.

Put the below code in global.asax under Application_Error().

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
Dim currentException As Exception
currentException = Server.GetLastError.GetBaseException()
Response.Redirect("/error.aspx?Err=" & Server.UrlEncode(currentException.Message))
End Sub

and, create 2 file called error.aspx and error.aspx.vb


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = Request.QueryString("Err")
End Sub


< asp: Label ID="Label1" runat="server" Text="Label" style="color: #FF0000; font-size: small; font-weight: 700">

Also, you could use the ‘try..catch’ or throw exception methods as well.
e.g; Check the attachment field is not empty, if its empty them throw the error.

If String.IsNullOrEmpty(yourattachmentfieldid.FileName) OrElse yourattachmentfieldid.PostedFile Is Nothing Then
Throw New ApplicationException(“Attachment filed is empty!!”)
End If