Results 1 to 20 of 20

Thread: error handling -> causes any performance issues?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    error handling -> causes any performance issues?

    I'm wrapping most parts of my code in a lot of try catch statements (wherever I can heh)
    I'm wondering if using too many try catch blocks causes any performance issues
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: error handling -> causes any performance issues?

    I believe it does but am unsure as to the extent. It was mentioned in a thread I was in before. Its when you nest multiple Trys that it will place a small drain on your resources. Try a search for it. I think it was less then 2 months ago.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: error handling -> causes any performance issues?

    It does, yes, but it also depends on what you do. You should make sure your Catches are as specific as possible, and progressively move towards more generic catches. (Catch SystemDBComplexException ex.... and later, Catch Exception ex).

    So you minimize the hit.

  4. #4

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: error handling -> causes any performance issues?

    aaah no nested ones but they're almost all Systme.Exception ones
    I dunno what errors can be raised... need more thought and consideration
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: error handling -> causes any performance issues?

    Also try only to use them when there is a decent possibility of a failure and sparingly too.

    Ps, Congrats on 4,004 posts
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: error handling -> causes any performance issues?

    heheh thanks 4005 now I guess
    umm to be honest I'm "scared" of unexpected error so I'm putting error handlers EVERYWHERE

    there's a lot of unexpected stuff.... sigh
    since you guys are saying there might be performance hits, I might reduce some of the error handling then
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: error handling -> causes any performance issues?

    You may be over thinking the issue. Its common and hard to retreat from. Data input, database connections and data transactions are probably the two most potential areas for exceptions.


    Ps, Congrats on 4005 posts
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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

    Re: error handling -> causes any performance issues?

    Further to what Rob said, there are a huge number of properties and methods that can throw exceptions but you are hardly going to wrap each in a Try block. For instance, indexing an ArrayList can throw an ArgumentOutOfRangeException, but who's going to allow for that under most circumstances? You would only employ exception handling on code sections that attempt things that are all or partly out of your control, like IO, data access and things like that. If you can't reasonably think of a scenario where your code could throw an exception then you generally shouldn't need to use exception handling. Then again, polite and reasonable are not the same thing. You should always consult the help topic for the current property or method to see what exceptions it can throw.
    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

  9. #9

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: error handling -> causes any performance issues?

    hehehehe
    thanks for teh advice all
    I'll see what kind of spaghetti code I can make
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: error handling -> causes any performance issues?

    You could just use lots of "On Error Resume Next" statements.


    PS: That was humor. Don't take it seriously. Seriously.

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: error handling -> causes any performance issues?

    Noteme gets really wound up when you mention Try{} blocks
    I don't live here any more.

  12. #12

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: error handling -> causes any performance issues?

    ok I feel stupid
    I'm calling Directory.CreateDirectory() and I could expect an IOException or an UnauthorizedAccessException to be thrown. So should I really create a catch block for both, and then a general one, in case another exception is thrown?

    Code:
    try
    {
           Directory.CreateDirectory()...
    			}
    			catch (IOException)
    			{}
    			catch (UnauthorizedAccessException)
    			{}
    			catch (Exception)
    			{}

    I know this sounds retarded, but I'm a confused person

    I would just go with catching a general System.Exception and nothing else
    what should I do

    edit: the reason I put multiple ones is because I thought it could be more efficient ? someone shoot me
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: error handling -> causes any performance issues?

    I think the idea of catching exceptions is for handling them. You should catch specific ones that you plan to handle in that specific way otherwise group the exception types with their associated handling. So if you are going to handle an UnauthorizedAccessException the same as an IOException then why catch them seperate? Also keep in mind that if no local error handler is found it bubbles up the call stack to find one. So some general errors should be caught in a common handler that way only specific things need to be caught else where in the application.

  14. #14

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: error handling -> causes any performance issues?

    Quote Originally Posted by Edneeis
    I think the idea of catching exceptions is for handling them. You should catch specific ones that you plan to handle in that specific way otherwise group the exception types with their associated handling. So if you are going to handle an UnauthorizedAccessException the same as an IOException then why catch them seperate? Also keep in mind that if no local error handler is found it bubbles up the call stack to find one. So some general errors should be caught in a common handler that way only specific things need to be caught else where in the application.
    okay
    I was under the wrong impression that if I catch specific ones rather than a general catch statement, it would make it more efficient. I don't really plan to do anything with the exception.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  15. #15
    Lively Member
    Join Date
    Sep 2005
    Location
    Glasgow, Scotland
    Posts
    77

    Re: error handling -> causes any performance issues?

    Actually, I'm also using Try...Catch extensively throughout my database application. Like Mr Polite, I'm so paranoid about my users doing things to break my application, every single subroutine that I write contains a Try...Catch clause. However, the only purpose for that being there is to remove the un-userfriendly error message, and throw it instead into the EventLog, replacing it on the screen with a friendlier-looking message.

    However, my application seems to be running quite slowly, even on my development laptop which is pretty powerful. All of my applications run from a module called "Mod_Main", and everything is called from that. If I put a single Try...Catch statement into that Mod_Main, will that then negate the need to have every single exception handled?

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

    Re: error handling -> causes any performance issues?

    Quote Originally Posted by uk_codemonkey
    Actually, I'm also using Try...Catch extensively throughout my database application. Like Mr Polite, I'm so paranoid about my users doing things to break my application, every single subroutine that I write contains a Try...Catch clause. However, the only purpose for that being there is to remove the un-userfriendly error message, and throw it instead into the EventLog, replacing it on the screen with a friendlier-looking message.

    However, my application seems to be running quite slowly, even on my development laptop which is pretty powerful. All of my applications run from a module called "Mod_Main", and everything is called from that. If I put a single Try...Catch statement into that Mod_Main, will that then negate the need to have every single exception handled?
    If you've written your code such that every method can throw an exception then you seriously need to rethink your design. Like I said previously, there are many properties and methods that can throw an exception, but you should have control over most of the circumstances that could cause those exceptions to be thrown. For instance, indexing a collection can throw an ArgumentOutOfRangeException, but if there is any possibility that the index is out of range then you should be using an If statement to test it first. Therefore there is no need for exception handling in this case. Be sensible and realistic with your exception handling. Perhaps you want to include a global exception handler so that your app will never generate an unhandled exception, but you should plan to never have to use it.
    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
    Lively Member
    Join Date
    Sep 2005
    Location
    Glasgow, Scotland
    Posts
    77

    Re: error handling -> causes any performance issues?

    Thanks to jmcilhinney for that reply. What I may well do is comment out a lot of the Try...Catch blocks for just now, and see if that causes the Exception to be handled in the Mod_Main subroutine. Hopefully the code'll run faster too
    Wise man once said: "Don't ever get married, just find a woman you don't like and buy her a house".
    According to ancient Chinese proverb, "Man with hole in pocket feels cocky all day".

  18. #18
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: error handling -> causes any performance issues?

    If your TRY/CATCH is a sub-routine size TRY/CATCH, similar to the ON ERROR GOTO that was used in the past, then I do not believe it changes the size or execution speed of anything.

    Every subroutine gets an error handler - regardless of whether you create it or if the compiler puts a default error handler into the object code.

    Having lots of TRY/CATCH's nested is a lot different then a TRY/CATCH for the overall subroutine.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  19. #19
    Lively Member
    Join Date
    Sep 2005
    Location
    Glasgow, Scotland
    Posts
    77

    Re: error handling -> causes any performance issues?

    Well actually, just as a test, I've just gone through the back-end code for one of the "busier" forms in my application, and commented out all of the Try...Catch...End Try statements, to see what would happen.

    As far as I can tell, removing those hasn't made one iota of difference to the speed with which the form reacts to things like drop-down list selections and button-clicks.
    Wise man once said: "Don't ever get married, just find a woman you don't like and buy her a house".
    According to ancient Chinese proverb, "Man with hole in pocket feels cocky all day".

  20. #20
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: error handling -> causes any performance issues?

    Quote Originally Posted by uk_codemonkey
    Well actually, just as a test, I've just gone through the back-end code for one of the "busier" forms in my application, and commented out all of the Try...Catch...End Try statements, to see what would happen.

    As far as I can tell, removing those hasn't made one iota of difference to the speed with which the form reacts to things like drop-down list selections and button-clicks.
    It would not affect the sppeed of drop down lists...

    Each TRY/CATCH adds a small amount of code to be executed - making the "code space" in memory larger - adding a couple of instructions - but only where they are "encountered" at run time.

    For a GUI type of form - speed would not be affected by TRY/CATCH use.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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