Porting VB code

In the case of err.raise(6) in vb: is that just stopping the program as there is no error handling being done?

In a nutshell the code is
If blah > 0 and blah < 7 then
code here
Else
err.raise(6)
end if
more code here.


According MSDN err.raise(6) ’ Generate an “Overflow” error.

TIA

The equivalent here is Raise Exception. http://documentation.xojo.com/index.php/Raise

In general when one raises an exception it is not to use error handling on it.

So it is stopping (shutting down) the program at this point and giving the appropriate error message. i.e. when I run the code from that link it gives me the error message and shuts down the program.

If you look at the Application.UnhandledException event you can keep your app from shutting down by returning true. Areas that are likely to have an exception should probably be wrapped in a try-catch block.

Thanks Bob, I’ve been changing it to returning a Boolean and skipping the error now that I know what it is. I just wasn’t sure what happened at that step when run as I’m porting from a text file and don’t have VB6 to check with.

It makes absolutely no sense to raise an exception to later handle it. That is consuming CPU to do nothing. Simply comment out the line containing the raise Exception instead.

Huh? how is that any different then skipping it and returning a boolean?

So you are not raising the error anymore? Good.

No, rather the user just enter the data over again and get it right :slight_smile: thx.

It was not so easy to guess what the code was supposed to do with the cryptic excerpt you posted (“code here”)…

As long as you are happy with the result, that’s fine.

Just needed to know if the err.raise stopped the code (which it does) or just trapped the error ran some other code to fix the data then continued with the code after the ‘End If’. Happy with knowing what to do :slight_smile: thx all

[quote=294131:@Jym Morton]In the case of err.raise(6) in vb: is that just stopping the program as there is no error handling being done?

[/quote]

If the VB6 program do an Err.Raise, the purpose is to better handle it properly in an Error Handling routine. Not to ignore it, stop the program or crash it. I believe there should be error handling routine some where in the codes.

I just referred to the book, Bug Proofing Visual Basic, by Rod Stephen who is an authoritative author, and managed to find this chapter of the book online:

http://www.vb-helper.com/tut6.htm

Scroll down to the middle and you will find Err.Raise and example of why and how it can be used to handle the error that is raised with more informative response to users…

Normally, blah (dim a byte or integer) outside of the 0-7 range do not trigger an overflow error 6. I assume blah must be within this 0-7 range or else it creates a problem somewhere else in the code therefore to be trapped and handled to prevent the program from crashing. Therefore if blah is less than 0 or greater than 7, the program do a Err.Raise overflow error number 6 to be handled somewhere.

You should not ignore any Err.Raise when converting the codes.

Sure, any serious program should have error handling.

However, using exceptions to manage program flow is bad form in any language. Exceptions handling is slower and CPU intensive. Besides, if effectively the error is supposed to be handled somewhere else like Unhandled Exception, this is an abhorrent non OOP structure. Not to mention unreadable code. Better create code to smoothly handle the else condition right there, or with a local method.

Public VB code should not IMHO be taken as an example of programming. Most of the time, it is meant to show feasibility, and has not been optimized. It can even not work at all.

Also, porting word for word from any language to Xojo does not take into account the specificity of Xojo and its most efficient aspects. It is just as awkward as, say, writing English by translating French word for word. Each language has its own programming style, call it music, that makes code beautiful and fast.

Jym has a VB6 source in a text file he is trying to convert to Xojo. He does not have VB6 to run it. What is in the text file is history and this is the history he is trying to understand so that he can convert to Xojo.

I am just trying to show him what Err.Raise may be in the history and that in the history there is probably somewhere he may find the handling code so that if he finds it, his converted Xojo codes will not crash unexpectedly.

I am not to judge the piece of history Jym has in his hand, something he has no control over.

I am also not here to judge Rod Stephens. I know he is good and I have some of his books since long time ago.
Rod Stephens

As far as I can tell, Jym never said it was from Rod Stevens. At any rate, I come from the prehistoric times or pre-Quickbasic and have been exposed to all sorts of programming styles, including by raising errors. That may have appeared like a nifty way to do things way back when. Today I would not use that.

Expressing one’s point of view does not mean judging past author’s methods.

Jym did not say it was from Rod Stephens. I show him example of how it may have been done with examples from Rod Stephens’ website.

I normally do not like to tell people this in case I may be mistaken for showing off - I have also been programming from long time back learning AppleSoft Basic on an Apple ][ EuroPlus. I have seen all sort of codes, all sorts of spaghetti that was done back in history.

Yes, i understand you were just expressing your point of view:

I was just trying to explain to Jym what the Err.Raise may do and ignoring it may lead to problem later in his conversion work.

What have I done to you?

Since we come both from the early days of AppleSoft (and Sinclair Basic before for me), and have had to discover concepts that today go (almost) without saying, we both know how important it is to try and use modern programming techniques.

Translating code can be a painful experience when languages are too different. In the case of Xojo vs VB, they are real close. In the case of err.Raise(), typing Raise in the LR points to http://documentation.xojo.com/index.php/Raise which describes the Xojo way, a quasi clone.

[quote=294201:@Cho Sing Kum]If the VB6 program do an Err.Raise, the purpose is to better handle it properly in an Error Handling routine. Not to ignore it, stop the program or crash it. I believe there should be error handling routine some where in the codes.

I just referred to the book, Bug Proofing Visual Basic, by Rod Stephen who is an authoritative author, and managed to find this chapter of the book online:

http://www.vb-helper.com/tut6.htm

Scroll down to the middle and you will find Err.Raise and example of why and how it can be used to handle the error that is raised with more informative response to users…[/quote]
@Cho Sing Kum
I think in this case the err.Raise(6) is just stopping the code (and program) from continuing. If it were actually handling the error, it wouldn’t be using (6) which is a preset number by Microsoft to raise an Overflow Error. https://msdn.microsoft.com/en-us/library/aa264525(v=vs.60).aspx No where in the code does it have any code for handling the error and I don’t think it will be out of the range 0-7 (a) because it can’t be less then zero due to using unsigned integers only and (b) I can’t see how it will ever be over 7 from all the math. I’m guessing it’s there just to be 101% sure. It’s an encryption algorithm and wouldn’t work if ‘blah’ is ever greater than 7 so really the program has to stop at this point anyway. In Rod’s example he is using (1001) which is very different then the first 512. https://msdn.microsoft.com/en-ca/library/w4t2e92e(v=vs.90).aspx

If there is no error handling for this then the overflow error 6 raised by the program will stop it unelegantly. Basically, Err.Raise is to intentionally create an error where there isn’t any. Therefore it can be given any number you want to the Err.Raise and the program will report that error number.

In the case of your codes example, the programmer wanted the program to raise an error whenever blah is outside the 0-7 range so maybe he thought to give it a 6 to represent “his overflow” condition.

In VB6, Er.Raise can be given any number that the programmer wanted, even a custom number to let him identify where he think his code is going wrong. This is why Rod used a custom 1001. He also use vbObjectError to change the in-built error description to his preference for the error to be described as Automation error rather than an Application error.

Do note I am not telling you how it should be done, but just maybe thinking why that was done in the first place. I can be guessing wrong.