-
Mar 17th, 2023, 04:58 AM
#1
Thread Starter
Hyperactive Member
[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
-
Mar 17th, 2023, 07:23 AM
#2
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
-
Mar 17th, 2023, 07:38 AM
#3
Thread Starter
Hyperactive Member
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
-
Mar 17th, 2023, 07:54 AM
#4
Re: Can we nesting multiple Try/Catches?
Originally Posted by pourkascheff
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.
-
Mar 17th, 2023, 08:14 AM
#5
Re: Can we nesting multiple Try/Catches?
Originally Posted by pourkascheff
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
...
Originally Posted by PlausiblyDamp
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
-
Mar 17th, 2023, 11:06 AM
#6
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
-
Mar 18th, 2023, 01:38 AM
#7
Thread Starter
Hyperactive Member
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.
-
Mar 18th, 2023, 03:21 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|