Results 1 to 8 of 8

Thread: [RESOLVED] Can we nesting multiple Try/Catches?

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Resolved [RESOLVED] Can we nesting multiple Try/Catches?

    Consider you want to get CPU temperature via WMI. A client mainboard does not support it, so it fails. Then you want to try a Performance Counter component and Thermal zone instance, instead. Most laptops does not support it neither. Finally you will try a third way that I'm working on it.

    My question is, does following statement programmatically sane? Do people usually do this? What are its weaknesses?
    Code:
    Try
    'DO SOMETHING BAD STAGE 1
    Catch ex As Exception
         Try
         'DO SOMETHING BAD STAGE 2
         Catch ex As Exception
              Try
              'DO SOMETHING BAD STAGE 3
              Catch ex As Exception
                   'NO WAY TO BE ACHIEVED
              End Try
         End Try
    End Try

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Can we nesting multiple Try/Catches?

    My question is, does following statement programmatically sane? Do people usually do this? What are its weaknesses?
    No.
    No. (I hope not!)
    Difficult to follow...

    What I've done in similar situations is this:
    Code:
    Try
      someObject = tryThis()
    Catch ex as Exception
      'log it
    End Try
    
    if someObjerct is Null then
      Try
        someObject = tryThis2()
      Catch ex as Exception
        'log it
      End Try
    End If
    
    if someObjerct is Null then
      Try
        someObject = tryThis3()
      Catch ex as Exception
        'log it
      End Try
    End If
    
    if someObject is Null Then
      'Something has gone completely wrong and everything failed.
    else
      'It's all good...
    End If
    By using nested try...catch (and yes, you can nest them) you're burrying normal processing logic in your exception handlers. Instead you should actually handle the error by logging it, and then continuing on... you can check your return variable to see if you actually got something back - or you can set a flag, just remember to reset it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: Can we nesting multiple Try/Catches?

    Very good idea! Thanks for sharing this approach.

    I would be delighted If you describe a bit more about someObject type and how could it be a byte how to make a default value (for boolean) sir? Option Infer = On

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Can we nesting multiple Try/Catches?

    Quote Originally Posted by pourkascheff View Post
    Consider you want to get CPU temperature via WMI. A client mainboard does not support it, so it fails. Then you want to try a Performance Counter component and Thermal zone instance, instead. Most laptops does not support it neither. Finally you will try a third way that I'm working on it.

    My question is, does following statement programmatically sane? Do people usually do this? What are its weaknesses?
    Code:
    Try
    'DO SOMETHING BAD STAGE 1
    Catch ex As Exception
         Try
         'DO SOMETHING BAD STAGE 2
         Catch ex As Exception
              Try
              'DO SOMETHING BAD STAGE 3
              Catch ex As Exception
                   'NO WAY TO BE ACHIEVED
              End Try
         End Try
    End Try
    I would also avoid catching Exception, if you know what kind of Exception you are expecting then catch the specific exception(s) and deal with what you know.

    Catching just Exception can result in unexpected errors being caught and dealt with in the wrong way.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Can we nesting multiple Try/Catches?

    Quote Originally Posted by pourkascheff View Post
    Very good idea! Thanks for sharing this approach.

    I would be delighted If you describe a bit more about someObject type and how could it be a byte how to make a default value (for boolean) sir? Option Infer = On
    It can still be a Byte ... but you should test something else... this is where the boolean flag comes in...

    Code:
    Dim success as Boolean
    
    Try
      someByte = tryThis()
      success = True
    Catch ex as Exception
      'log it
      success = False
    End Try
    
    if not success then
      Try
        someByte = tryThis2()
        success = True
      Catch ex as Exception
        'log it
        success = False
      End Try
    End If
    
    if not success then
      Try
        someByte = tryThis3()
        success = True
      Catch ex as Exception
        'log it
        success = False
      End Try
    End If
    
    ...

    Quote Originally Posted by PlausiblyDamp View Post
    I would also avoid catching Exception, if you know what kind of Exception you are expecting then catch the specific exception(s) and deal with what you know.

    Catching just Exception can result in unexpected errors being caught and dealt with in the wrong way.
    Yeah, if you know what exception(s) can be thrown, it's best to catch those specifically ...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Can we nesting multiple Try/Catches?

    I have used a nested exception, but only a once or twice over the last few decades. The few times I have done it was when there was something significant that could be done in the exception handler. The only one I remember was that there was a somewhat older means of making web requests where certain, otherwise valid, responses from the server would manifest as exceptions. It might have been due to the way that particular site was handling things, but I was asking for A, and if the endpoint decided that A wasn't available, it would respond with what ended up being an exception. Therefore, I had to take a close look at the exception to decide whether it was really an exception or actually a successful, though negative, response. All the logic to decide whether an exception was actually an exception also had the potential to raise exceptions, so I did nest exception handlers in that case. Normally, though, I would use a construct like what TG displayed.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: Can we nesting multiple Try/Catches?

    When shaggy tried it, it would be considered as a safe experience but I know it depends. What I've learned from Try/Catch is sometimes I feel they take a bit more time to accomplish which could be a good thing. I don't care it takes 10ms or 10'000ms. Not sure about other ways of using Try e.g. someByte = tryThis(). I will close the case today.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: [RESOLVED] Can we nesting multiple Try/Catches?

    An exception handler costs nothing unless there's an exception. If there's an exception, it is terribly expensive. Therefore, you should only use exception handling to handle exceptional situations. Exceptional situations are those that could arise, but which you can't avoid by testing for them.
    My usual boring signature: Nothing

Tags for this Thread

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