dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Alternative for "On Error Resume Next" in VB.NEt

  1. #1
    Join Date
    Sep 2004
    Posts
    265

    Unhappy Alternative for "On Error Resume Next" in VB.NEt

    Hi

    I was using "On Error Resume Next" in VB to avoid certain errors.

    I want to know the equivalent of "On Error Resume Next" in VB.Net.

    Thanking in advance

  2. #2
    Join Date
    Jun 1999
    Posts
    153

    Re: Alternative for "On Error Resume Next" in VB.NEt

    There isn't any.
    Kevin

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,264

    Re: Alternative for "On Error Resume Next" in VB.NEt

    Try and Catch blocks would be the more prefferred and appropriate way of Error handling Rahul.
    have a look at these FAQ's, Exception Handling is covered there as well :
    http://www.codeguru.com/forum/showthread.php?t=368148

    More specifically, here's the link to the Exception Handling FAQ :
    http://www.codeguru.com/forum/showthread.php?t=383057

    Hope it helps!
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  4. #4
    Join Date
    Sep 2004
    Posts
    265

    Unhappy Re: Alternative for "On Error Resume Next" in VB.NEt

    In VB.NEt

    How can I continue execution even if an exception occurs. (I don't want the exception to be displayed on the screen). The application should continue executing the next line of code.

    Thanking in advance

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,264

    Re: Alternative for "On Error Resume Next" in VB.NEt

    Did you read my previous post in this Thread, post #3, Rahul ¿
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  6. #6
    Join Date
    Apr 2005
    Posts
    576

    Re: Alternative for "On Error Resume Next" in VB.NEt

    It is supported just like in VB 6.0 via "unstructured error handling", however it is recommended to use structured error handling (try/catch/finally) both for performance and maintenance reasons.

    Try this code in a sub:

    Code:
         On Error Resume Next
         Throw New ApplicationException("Ooops")
         MessageBox.Show("resuming next")
         On Error GoTo handler
         Throw New ApplicationException("error again")
         MessageBox.Show("Exiting sub")
         Exit Sub
    handler:
         MessageBox.Show("Caught exception " + Err.GetException().Message)
         Resume Next

  7. #7
    Join Date
    Jul 2006
    Location
    At home
    Posts
    70

    Re: Alternative for "On Error Resume Next" in VB.NEt

    Try/Catch/Finally blocks are a much safer way to go.

  8. #8
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Alternative for "On Error Resume Next" in VB.NEt

    I want to know the equivalent of "On Error Resume Next" in VB.Net.
    Answer: "On Error Resume Next" still works.


    Depending on the situation, it's usually better to use the new
    Try, Catch, Finally, End Try. It requires you to do a little more thinking about possible errors, and how to handle them specifically.

  9. #9
    Join Date
    Aug 2005
    Posts
    198

    Re: Alternative for "On Error Resume Next" in VB.NEt

    The Try/Catch equivalent to On Error Resume Next is to have the following around every line where you want errors/exceptions ignored:
    Try
    ...
    Catch
    'do nothing
    End Try

    If it's only one or two lines, then this is fine.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  10. #10
    Join Date
    Nov 2004
    Location
    LA. California Raiders #1 AKA: Gangsta Yoda™
    Posts
    616

    Re: Alternative for "On Error Resume Next" in VB.NEt

    You shouldnt hold on to the old VB 6 ways of programming. You need to read the posted links and then you will see without a doubt why you should use Try/Catch/Finally and trash the VB 6 style On Error
    VB/Office Guru™ (AKA: Gangsta Yoda™)
    VB Forums - Super Moderator 2001-Present

    Microsoft MVP 2006-2011

    Please use [code]your code goes in here[/code] tags when posting code.

    Senior Software Engineer MCP, BSEE, CET
    VS 2012 Premium, VS 6.0 Enterprise SP6, VSTO, Office Ultimate 2010, Windows 7 Ultimate
    Star Wars Gangsta Rap SE Reputations & Rating Posts Office Primary Interop AssembliesAdvanced VB/Office Guru™ Word SpellChecker™.NETAdvanced VB/Office Guru™ Word SpellChecker™ VB6Outlook Global Address ListVB6/Crystal Report Ex.VB6/CR Print Setup Dialog Ex.

  11. #11
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Alternative for "On Error Resume Next" in VB.NEt

    If it's only one or two lines, then this is fine.
    Glad you included that, since doing nothing is not exactly the same.

    Some sets of programming statements, will still throw an error window for the "Just in time compilier", even if you do nothing in the catch.
    As I stated, in most situations use try/catch, and in very few use"On error resume next" if you know that each line statements is okay to bypass on an error. This is not unsafe in any way, in these cases.

  12. #12
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Alternative for "On Error Resume Next" in VB.NEt

    On Error Resume Next is lame whatever the weather..

    Why not try doing a test first to see if the operation will fail, and then dont do it if it will!?


    If myObject IsNot Nothing then myObject.doSomething()


    rather than:
    try
    myObject.doSomething()
    catch NullReferenceException
    end try
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  13. #13
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Alternative for "On Error Resume Next" in VB.NEt

    cjard,
    Yes that makes sense, to always to that sort of thing, as a form of defensive programming.

    What conditions are you refering to, when you denounce the
    "On error resume next" ?
    I guess all of them, but could you provide an example, where something bad happens?

  14. #14
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,264

    Re: Alternative for "On Error Resume Next" in VB.NEt

    Why not try to do Error Avoidance instead of Error Handling ¿
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)