[RESOLVED] Can someone explain .net's error handling to me?
I just don't understand a few things about error handling.... for example...
Why does the following raise an error in a button_click event and not on a form_load event (in the form load event all lines that occur after the erroneous line are not evaluated):
(btw I know that an error is raised if i put a try / catch round it ... the above... but why does it act differently if i don't?)
Code:
Dim x As Integer = 22
Dim asd As Integer = x / 0
And why does doing the same code (above) in a button click in another of my applications not cause an error to raise and close the form?
Thanks in advance
Kris
Re: Can someone explain .net's error handling to me?
Where is this code? Is it in the button_click or in the form_load?
Re: Can someone explain .net's error handling to me?
In both - the line in red is a breakpoint ... as u can see it doesn't get hit
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x As Integer = 22
Dim asd As Integer = x / 0
msgbox "asd"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer = 22
Dim asd As Integer = x / 0
End Sub
End Class
Re: Can someone explain .net's error handling to me?
this is just a test project to show how s**t .net error handling is :)
Re: Can someone explain .net's error handling to me?
Since you have already made a determination as to the quality of .NET's error handling, despite your confession of not knowing it, I don't know that anyone can help you. Perhaps, you should just read MSDN.
http://msdn.microsoft.com/en-us/libr...ab(VS.80).aspx
Re: Can someone explain .net's error handling to me?
Quote:
Originally Posted by
i00
In both - the line in red is a breakpoint ... as u can see it doesn't get hit
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x As Integer = 22
Dim asd As Integer = x / 0
msgbox "asd"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer = 22
Dim asd As Integer = x / 0
End Sub
End Class
Obviously the line won't be hit because the error occurred before it. You would need to handle that error to continue.
This is the way almost all languages work. They will get stuck at error line and won't continue unless you have take care of that while coding that it should continue inspite of any error occuring.
The other way (not recommended), just in case you are so desperate to show that message box despite the error is to ignore the error using ON ERROR RESUME NEXT
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next '<-- insert this here to bypass all errors.
Dim x As Integer = 22
Dim asd As Integer = x / 0
msgbox ("asd")
End Sub
Re: Can someone explain .net's error handling to me?
yes i understand that lines after the error don't get executed - but what i want to know is why does the error catching in the developer environment work differently on form load and button click?
Kris
Re: Can someone explain .net's error handling to me?
also every (modern) language i have known takes u to the line of code not just ignores it and every other line following if you don't catch an error - but only when it feels like it
Re: Can someone explain .net's error handling to me?
Well, for one thing, if you had option strict on (as I know you have been prompted, on several occasions, to turn on), then your code wouldn't have compiled, because such division results in a double, yet you are assigning it to an integer. If you had used a double, the resulting value from the division would be #INF (Double.PositiveInfinity).
Why are you so quick to try to prove to us that .NET isn't good, yet you refuse to take certain tips?
Re: Can someone explain .net's error handling to me?
Altough more related to the actual issue. If you look at the output window during runtime, you'll see that as soon as execution reaches the division-by-zero line in Form_Load, you'll get this in the output:
A first chance exception of type 'System.OverflowException' occurred in MyApplication.exe
This means that somewhere deep down in the .NET code, the error was trapped internally. Read more about First chance exceptions here.
Re: Can someone explain .net's error handling to me?
Exceptions thrown in the Load event handler are caught in system code.
http://social.msdn.microsoft.com/For...b-ff71d67a0022
I simply googled exception form load .net and that was the first result. If I can do that...
3 Attachment(s)
Re: Can someone explain .net's error handling to me?
Have a look at these screenshots. Isn't this what you are expecting? Or you are trying to say something else?
In Debugger:
Attachment 72918
Attachment 72917
In Compiled EXE:
Attachment 72919
Re: Can someone explain .net's error handling to me?
Pradeep1210 - thats weird ... the 1st screen shot u posted doesn't break in mine - only does on the button click
- how do i get it to do that?
Thanks
Kris
Re: Can someone explain .net's error handling to me?
Quote:
Originally Posted by
i00
Pradeep1210 - thats weird ... the 1st screen shot u posted doesn't break in mine - only does on the button click
- how do i get it to do that?
Thanks
Kris
What OS are you on? I just tried on XP at work and it worked like it did for Pradeep, who is also obviously on XP. I'm sure it didn't work like that on my Win7 system at home, and Vista would likely behave like Win7.
Re: Can someone explain .net's error handling to me?
I think Debug > CLR Exceptions > Thrown needs to be checked
Re: Can someone explain .net's error handling to me?
Quote:
Originally Posted by
ForumAccount
I think Debug > CLR Exceptions > Thrown needs to be checked
Nope. It's not on my work system but I see the same behaviour as Pradeep reported.
Re: Can someone explain .net's error handling to me?
thanks a heap ForumAccount - weird that this isn't checked by default :)
Kris
Re: Can someone explain .net's error handling to me?
Quote:
Originally Posted by
jmcilhinney
What OS are you on? I just tried on XP at work and it worked like it did for Pradeep, who is also obviously on XP. I'm sure it didn't work like that on my Win7 system at home, and Vista would likely behave like Win7.
Yes I'm on Windows XP SP2 and using Visual Studio 2008 SP1.
Re: [RESOLVED] Can someone explain .net's error handling to me?
dividing anything by 0 really causes an error i think?