views:

66

answers:

3

Could some body explain exception vs error in asp.net

A: 

They both the same thing. An exception happens in "exceptional circumstances" i.e. the application cannot carry on, which is an error.

m.edmondson
+2  A: 

I'm assuming you're talking about the On Error statement in VB.NET which is just an "unstructured" way of handling Exceptions in VB.NET (it was carried over from the VB syntax to make things more familiar for VB developers).

Structured Error handling (using try/catch blocks) is the more accepted way of Exception handling in your code.

Take, for example, the following VB.NET Code:

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo nextstep
        Dim result As Integer
        Dim num As Integer
        num = 100
        result = num / 0
nextstep:
        MsgBox("Control Here")
    End Sub
End Class

Could be re-written using structured Exception handling quite easily:

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim result As Integer
        Dim num As Integer
        Try
            num = 100
            result = num / 0
        Catch ex As Exception
            MsgBox("Control Here")
        End Try
    End Sub
End Class
Justin Niessner
+1. but I'd add that you should not use the 'On Error' statement if you're just learning. the try...catch block is much more accepted, and is a better way of handling errors. (And it's the pattern you'll see in Java, C#, and other popular languages, so learning it now will help make learning those languages easier.) As the answer said, it was carried over from VB6 syntax, as a way of keeping things familiar to VB6 developers who were converting to .NET, but IMHO it's outlived its usefulness and should be avoided.
David Stratton
A: 

Some people use error to refer to an unrecoverable discrepancy. Some use error to mean any possible issue.

I would say that an error is anytime a program deviates from the expected flow. So you could say they are exceptions to what the program was intended for. I would also say that exceptions in the programming sense are anything that needs to be handled in a way different from the general case, which includes some, but is not limited to, errors. Some errors can't be handled.

Exception handling can be used for more than just catching undesirable effects.

While this is not a very detailed answer, at least it points out that they are not equivalent.

If all Zims are Zoms and all Zigs are Zoms, are all Zims also Zigs? Are all Zigs also Zims?

Robolulz