|
-
Aug 31st, 2009, 06:14 AM
#1
[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
-
Aug 31st, 2009, 06:18 AM
#2
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?
-
Aug 31st, 2009, 06:39 AM
#3
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
-
Aug 31st, 2009, 06:40 AM
#4
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
-
Aug 31st, 2009, 06:57 AM
#5
Hyperactive Member
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
Ben
Using Visual Basic 2005/2008
-
Aug 31st, 2009, 07:09 AM
#6
Re: Can someone explain .net's error handling to me?
 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
-
Aug 31st, 2009, 07:14 AM
#7
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
-
Aug 31st, 2009, 07:21 AM
#8
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
-
Aug 31st, 2009, 07:40 AM
#9
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?
Last edited by Atheist; Aug 31st, 2009 at 07:57 AM.
-
Aug 31st, 2009, 07:48 AM
#10
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.
-
Aug 31st, 2009, 07:56 AM
#11
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...
-
Aug 31st, 2009, 08:40 AM
#12
-
Aug 31st, 2009, 07:27 PM
#13
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
-
Aug 31st, 2009, 07:34 PM
#14
Re: Can someone explain .net's error handling to me?
 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.
-
Aug 31st, 2009, 07:41 PM
#15
Re: Can someone explain .net's error handling to me?
I think Debug > CLR Exceptions > Thrown needs to be checked
-
Aug 31st, 2009, 07:48 PM
#16
Re: Can someone explain .net's error handling to me?
 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.
-
Aug 31st, 2009, 07:59 PM
#17
Re: Can someone explain .net's error handling to me?
thanks a heap ForumAccount - weird that this isn't checked by default 
Kris
-
Sep 1st, 2009, 02:08 AM
#18
Re: Can someone explain .net's error handling to me?
 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.
-
Sep 1st, 2009, 04:22 AM
#19
Junior Member
Re: [RESOLVED] Can someone explain .net's error handling to me?
dividing anything by 0 really causes an error i think?
-
Sep 1st, 2009, 08:19 PM
#20
.net error handeling
A few days ago i posted asking about why the following wasn't throwing on the form load event but was on the button click:
Code:
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
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
I was told that I could make this throw in both places by going to "Debug>Exceptions" and then checking Thrown under "Common Language Runtime Exceptions"
This does make the error throw in both places however it also makes the errors throw in a try/catch block such as this:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim x As Integer = 22
Dim asd As Integer = x / 0
Catch ex As Exception
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim x As Integer = 22
Dim asd As Integer = x / 0
Catch ex As Exception
End Try
End Sub
How can i make the errors only throw (in both places) if they are not handled?
Thanks
Kris
-
Sep 1st, 2009, 08:23 PM
#21
Re: .net error handeling
Please don't create a new thread for the same topic, although I guess if you prematurely resolved that thread it could be a problem. I asked you a question in that thread of yours and you are yet to answer it. Maybe a link to the original thread would be a good idea too.
-
Sep 1st, 2009, 09:11 PM
#22
Re: .net error handeling
Yea i did prematurely mark it as resolved - as i didn't realize that doing this would throw errors in try catches.
Good point about the link to the original post it can be found here.
I am on vista btw
Kris
-
Sep 1st, 2009, 09:15 PM
#23
Re: .net error handeling
 Originally Posted by i00
I am on vista btw
Then I'm not sure that you can do what you're asking for. The behaviour seems to be different on XP comapred to Vista/Win7. Pradeep and I both saw the behaviour you're asking for on XP but I also saw the same behaviour you're seeing when I tried on Win7. It seems like exceptions thrown in the Load event handler of the startup form (maybe not others) are handled by the system on Vista and Win7 but not on XP.
-
Sep 1st, 2009, 09:33 PM
#24
Re: .net error handeling
Yes i just checked in windows xp (on another computer at work) and you are right
- so I was right in my other post the error handling in .net is inconsistent 
- and why would the operating system handle these?
Thanks
Kris
-
Sep 1st, 2009, 09:34 PM
#25
Re: .net error handeling
also in vb6 the same thing works as it should in vista (ie. throws errors) - so wouldn't it be something to do with the framework?
Thanks
Kris
-
Sep 2nd, 2009, 06:02 AM
#26
Re: .net error handeling
Ahhh this explains some problems I had recently (I'm on Vista too) where exceptions were not being raised in the form load event. I had done the same as you and ticked the CLR exceptions box but then had the exact same problem as you where the exceptions got thrown all the time even if I handled them.
So it looks like this is another great 'feature' of Vista eh? Perhaps one of you MVPs could ask someone at MS why this is the case and if anything can be done about it?
-
Sep 2nd, 2009, 06:25 AM
#27
Re: .net error handeling
 Originally Posted by i00
A few days ago i posted asking about why the following wasn't throwing on the form load
Both threads merged
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|