FreeBasic Try-Catch-Throw feature for win only

Windows specific questions.
Post Reply
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

FreeBasic Try-Catch-Throw feature for win only

Post by marpon »

I'm pleased to share my new piece of code : Try-Catch-Throw for Freebasic (for win 32 & 64)
thanks to St_W and others here in this forum who gave me the initial input...

That implementation is able to work on infinite nested Try-Catch-Finally-EndTry blocks (Finally is optionnal)

you will have under an include file + a test file

the class is using static members, simplifying the management
i used a "sort" of simplified singleton protected by Static countID to avoid the class been instancied more than 1 time

the include file has 2 parts the top is the common part (like a .bi file) used to declare only
the second part protected by # ifdef _EXCEPTIONS_INIT_CODE_ is the definition body for class and functions/subs

if you use single .bas file compilation : main.bas file and included code files:
put the # define _EXCEPTIONS_INIT_CODE_ before the #include once "Exception_Management_Win.inc" in that main.bas file
the example is like that

if you use multiple modules .bas files (compiled to .o files) as c is used to do , multi-module project
each of the .bas file using the exception management needs the #include once "Exception_Management_Win.inc"
and the "main.bas" will have the # define _EXCEPTIONS_INIT_CODE_ before the #include once "Exception_Management_Win.inc"

doing like that, it prevents the multiple definition error at compile time
and initialises the class to catch the exceptions even before the first TRY in the code.

You could find the evolutions via the link in Paul Squires forum
http://www.planetsquires.com/protect/fo ... pic=4118.0


note: if you want to trap the normal errors with that code , not use -exx in your compilation flags ( it probably adds a trapp mechanism)
but can use -w pedantic flag without any problem

in any case: your own exceptions will always be catched , whatever compilation flag you use.
Imortis
Moderator
Posts: 1923
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: FreeBasic Try-Catch-Throw feature for win only

Post by Imortis »

Interesting. I seem to remember something similar from many years ago, though it was much more limited. I will need to look at this later to see how it works.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: FreeBasic Try-Catch-Throw feature for win only

Post by vdecampo »

jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: FreeBasic Try-Catch-Throw feature for win only

Post by jj2007 »

From the thread linked by Vince:
Cherry wrote:I wonder why nobody is interested in this.
I have invested considerable efforts to implement Try/Catch/Finally for my own baby. Works fine but indeed, nobody is interested, including myself.

The issue here is mainly the size of a program. Normally, you don't need exception handlers. You WANT that exceptions blow in your face, because that saves you the effort to find the bug. But if your code exceeds, say, 50,000 lines, and you use half a dozen third party libraries, then you might reach a point where you lose the full control over your code, and need to prepare for the "oops, I am completely lost" case: catch the exception, save data, blame the user, whatever.

Therefore I consider Try/Catch/Finally a useful feature, and congratulate the authors. But sit together and coordinate your efforts ;-)
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: FreeBasic Try-Catch-Throw feature for win only

Post by marpon »

@vdecampo
i've already tested the code on that previous post but none of the versions were working with fbc 64,
mine yes, as with unlimited (unamed) nested try-catch

it could easily be implemented also for linux, using the signal function to (try to) trap the system errors,
on windows the vectored solution is interresting , linux would just implement signal management.


@jj2007 , dont understand your point, you are saying first
"nobody is interested, including myself." more comments to explain why is not interresting according your view
continue with
"Therefore I consider Try/Catch/Finally a useful feature, and congratulate the authors."
finish with
"But sit together and coordinate your efforts ;-)"

why ? if nobody is interested ;-)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: FreeBasic Try-Catch-Throw feature for win only

Post by jj2007 »

The point is that it would be useful for bigger projects, with external not-so-well documented libraries etc.; but it's not very helpful to have two half-ready versions with a conflicting syntax. Try to combine the best elements of Cherry's version with yours, then make it much, much simpler, and FreeBasic has one valuable feature more.
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: FreeBasic Try-Catch-Throw feature for win only

Post by marpon »

@jj2007 thanks for your comments it is showing your interrest
but it's not very helpful to have two half-ready versions with a conflicting syntax.
what do you mean by half-ready versions : what's wrong with my proposal?
did you find bugs, wrong behaviour... ?
elaborate more please , do not hesitate to give some samples, that will help me to understand what you need more.

conflicting synthax : sure they use different way for the same objective, why do you want to combine them? choose the one you prefer...
then make it much, much simpler
you are welcome to show us what you have in mind, no problem for me to learn and use simpler /elegant ways .
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: FreeBasic Try-Catch-Throw feature for win only

Post by jj2007 »

marpon wrote:what's wrong with my proposal?
did you find bugs, wrong behaviour... ?
...
no problem for me to learn and use simpler /elegant ways .
Hi marpon,
Nothing is wrong with your proposal, but the instructions are somewhat complicated, which is often a sign of "work in progress".
How the final result should look like, is open for debate. Try a Google search for try catch finally, what pops up are Java, php, Python, C# examples - pick the simplest syntax...

Here is one real example, for inspiration:

Code: Select all

include \masm32\MasmBasic\MasmBasic.inc
  SetGlobals divisor=0
  Init tc	; initialise MasmBasic with Try/Catch (aka SEH)
  mov eax, 123	; use 123 as the dividend 
  cdq			; and sign-extend it to edx
  Try
	div divisor	; for divisor=0, that looks illegal, folks!
	PrintLine "Hey, that worked"	; you won't see this line
  Catch
  	PrintLine Str$("Ouch, we had an exception in source line %i at\nAddress\t", LastEx(line)), Hex$(LastEx(addr)), CrLf$, "Code", Tb$, Hex$(LastEx(code)), CrLf$
  Finally
  MsgBox 0, "OK?", "SEH is easy", MB_OK
EndOfCode
Console output (plus the MessageBox, of course):

Code: Select all

Ouch, we had an exception in source line 7 at
Address 004010C4
Code    C0000094
When writing "keep it simple", I mean "don't fall into the trap of designing sophisticated mechanisms to handle exceptions; because such handlers are hacks that serve to hide lousy programming behind apparently user-friendly message boxes" ;-)
Post Reply