Results 1 to 27 of 27

Thread: [RESOLVED] Can someone explain .net's error handling to me?

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Resolved [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

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    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?
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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

  4. #4

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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

  5. #5
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    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

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Can someone explain .net's error handling to me?

    Quote Originally Posted by i00 View Post
    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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

  8. #8

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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...
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    Name:  loaderr.JPG
Views: 361
Size:  84.9 KB
    Name:  btnclickerr.JPG
Views: 367
Size:  91.4 KB

    In Compiled EXE:
    Name:  exeloaderr.JPG
Views: 365
Size:  186.4 KB
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can someone explain .net's error handling to me?

    Quote Originally Posted by i00 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Can someone explain .net's error handling to me?

    I think Debug > CLR Exceptions > Thrown needs to be checked

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can someone explain .net's error handling to me?

    Quote Originally Posted by ForumAccount View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Can someone explain .net's error handling to me?

    thanks a heap ForumAccount - weird that this isn't checked by default

    Kris

  18. #18
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Can someone explain .net's error handling to me?

    Quote Originally Posted by jmcilhinney View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  19. #19
    Junior Member
    Join Date
    Feb 2007
    Location
    Around the Fur
    Posts
    19

    Re: [RESOLVED] Can someone explain .net's error handling to me?

    dividing anything by 0 really causes an error i think?

  20. #20

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    .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

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  22. #22

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: .net error handeling

    Quote Originally Posted by i00 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  24. #24

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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

  25. #25

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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

  26. #26
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  27. #27
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: .net error handeling

    Quote Originally Posted by i00 View Post
    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
  •  



Click Here to Expand Forum to Full Width