Why is this Try/Catch Block Not Working?

Hey all,

I have the following code:

      Try
        s = ReadAll(Encodings.UTF8)
        Dim d1() as String = s.Split("AUD_DLY_READ_CURRENT"+EndOfLine.Windows)
        Dim d() as String = d1(d1.Ubound).Split(EndOfLine.Windows)
        Delay = d(0)
        
        If Delay = "" Then
          Delay = d(50) ' Force an exception here
        End If
        
      Catch
        mRequestedHardwareType = False
        mTImerPeriod = mTImerPeriod+50
        t.Period = mTImerPeriod
        t.Reset
      End Try

In the spot where I as if Delay = “” and then intentionally force an exception, I am expecting the catch block to handle the exception. It’s not. Instead an exception is being raised.

I can step through in the debugger and the code in the catch block never executes.

What’s wrong with what I am doing? I thought it had been working previously, but tonight it seems like Try/Catch blocks are being ignored…

I am wondering if this is an IDE/debugging issue. I put some logging statements into my code indicating I had entered the catch block and compiled and ran it. The logging statements showed up. So It does appear the catch block is working but not in the IDE…

Do you have
#pragma BreakonException Off
before the try to tell the debugger to ignore the exception?

…or, when the exception occurs, just click ‘resume’ to cause the code to continue executing

[quote=161226:@Wayne Golding]Do you have
#pragma BreakonException Off
before the try to tell the debugger to ignore the exception?[/quote]

I did but the exception handler I have for unhandled exceptions in my app was being thrown which caused me to investigate.

Having #pragma breakonexceptions off should not stop code from switching to the catch block.

No. You miss the point. The catch block code is not executing.

The debugger was not allowing the Try/Catch block to handle the exception.

If you run it with BreakOnExceptions ON does it break into the debugger on this line…?

Delay = d(50)

And then if you Step-In you’re saying that it jumps to App.UnhandledException instead of the first line after Catch?

[quote=161235:@Will Shank]If you run it with BreakOnExceptions ON does it break into the debugger on this line…?

Delay = d(50)

And then if you Step-In you’re saying that it jumps to App.UnhandledException instead of the first line after Catch?[/quote]

Actually not always. Most of the time it just goes down to the next line after the catch block.

Have you tried catching the specific exception?

Try ~ Catch err as NilObjectException ~ End Try

Don’t be tempted to catch all exceptions.

Catch err as RuntimeException

It prevents threads from being threaded.

In this particular instance, the try/catch block is in the event handler of a timer. So no threading involved here.

It’s totally an IDE/debugger thing. Once I did a build of the app and compiled it, now it works fine in the IDE. It had to be some corrupt code in a cache somewhere or something…

Jon, was this just example code or are you really using this method to raise an exception? And in the particular code you posted, why not just use an Else block?

(If it was just for illustration, disregard.)

[quote=161248:@Kem Tekinay]Jon, was this just example code or are you really using this method to raise an exception? And in the particular code you posted, why not just use an Else block?

(If it was just for illustration, disregard.)[/quote]

Yes, and there’s a specific reason for it.

I’m reading a parameter from a device via a timer. I have an estimate on how many milliseconds it takes on average for the device to respond. But some devices are quicker than others in their response time. So the response time is not consistent. In this section of the code:

s = ReadAll(Encodings.UTF8)
        Dim d1() as String = s.Split("AUD_DLY_READ_CURRENT"+EndOfLine.Windows)
        Dim d() as String = d1(d1.Ubound).Split(EndOfLine.Windows)
        Delay = d(0)

s is the data that was read from the socket at the start of the timer. This text below is an actual telnet connection via a shell that shows the output:

BusyBox v1.10.3 (2014-01-17 10:48:09 CST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

/usr/local/bin # AUD_DLY_READ_CURRENT
2
/usr/local/bin # 

Due to the way data comes into a TCP socket, it doesn’t all come in at once. So when the timer fires, s could be a blank string. It could have that whole bit above. Or part of it.

So the bottom line is that when I say:

Delay = d(0)

d(0) might raise an out of bounds exception. This was the original purpose of the catch block which then lengthens the period of the timer and resets it. The timer’s action method then runs again and we eventually get the right data. d(0) could be a null string which is a perfectly valid value in the framework. But I don’t want to use it. So instead of creating an If/Then block and duplicating the catch block (which needs to be there anyhow), I just purposely raise the exception and cause the code to move to the Catch Block.

So there’s two possible places where I could get what is effectively invalid data and I need to account for each of them. One of those places will cause an exception. The other I raise the exception just because the needed code is there anyhow.

That maybe, but I would recommend that you never do this. It took me weeks to figure out that my thread wasn’t working correctly because of it.

There’s a small notification in the language reference.

[quote=161252:@Sam Rowlands]That maybe, but I would recommend that you never do this. It took me weeks to figure out that my thread wasn’t working correctly because of it.

There’s a small notification in the language reference.[/quote]

It’s a good point. So how do you set up a catch block to catch a Nil Object Exception or an Out Of Bounds Exception.

Can you use an Or statement in the Catch line? I’ve always wondered about that.

[quote=161253:@Jon Ogden]It’s a good point. So how do you set up a catch block to catch a Nil Object Exception or an Out Of Bounds Exception.

Can you use an Or statement in the Catch line? I’ve always wondered about that.[/quote]
Ha… Since I discovered the tread blocking, I now only capture one runtime exception, I’ve never considered others! Maybe @Joe Ranieri or @Norman Palardy could explain how.

Yeah, that’s just it. In some cases I want to check for an Nil Object or an Out of Bounds. I am pretty sure you can have multiple catch blocks but then that gets inefficient…

You could also…

use goto :slight_smile:

put the catch code in a method and call that instead of duplicating

[quote=161257:@Will Shank]You could also…

use goto :slight_smile:

put the catch code in a method and call that instead of duplicating[/quote]

A pox be upon you.

try catch noe as NilObjectException // code catch oob as OutOfBoundsException // code end try

[quote=161259:@Norman Palardy]try catch noe as NilObjectException // code catch oob as OutOfBoundsException // code end try [/quote]

Yes. But in most of my cases in this example, I’ll want to use the exact same code. So now I have to create another method just to handle a couple lines of code. I end up with more lines of code, more methods to keep track of, etc.

If I could do:

try
catch noe as NilObjectException Or oob as OutofBoundsException
// code
End try

That would be nice.