[Win32] Exception handling using Try/Catch/Finally

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
stephaneweg
Posts: 1
Joined: Oct 20, 2016 17:55

Re: [Win32] Exception handling using Try/Catch/Finally

Postby stephaneweg » Oct 20, 2016 18:59

I just made a platform independant try catch mechanism for freebasic:



trycatch.bi

Code: Select all

type TryContext field=1
    Esp as unsigned integer
    Eip as unsigned integer
    Ebp as unsigned integer
end type

type Exception field=1
    TypeName as unsigned byte ptr
    Message as string
    Code as unsigned integer
    ObjData as any ptr
end type

dim shared ExceptionTypeName as unsigned byte ptr=@"Exception"
dim shared nbrTry as unsigned integer=0
dim shared Trys(0 to 255) as TryContext
declare function TRY() as Exception ptr
declare sub THROW(code as unsigned integer, msg as string,objData as any ptr)

#define BeginTry   do: var ex=Try(): select case ex: case 0:
#define Catch(v) nbrTry-=1: case else: var v=ex:
#define EndTry end select: loop while(0)


trycatch.bas

Code: Select all

function TRY() as Exception ptr
    dim _esp as unsigned integer
    dim _eip as unsigned integer
    dim _ebp as unsigned integer
    'saving the pointers (eip, ebp, and esp) to our table
    asm   
        push [ebp+4]
        pop [_eip]
        push [ebp+0]
        pop [_ebp]
        mov [_esp],ebp
    end asm
    Trys(nbrTry).esp=_esp
    Trys(nbrTry).ebp=_ebp
    Trys(nbrTry).eip=_eip
    nbrTry+=1
'return 0 for the select case statement
    return 0
end function

sub THROW(errno as unsigned integer, msg as string,objData as any ptr)
    dim _esp as unsigned integer
    dim _eip as unsigned integer
    dim _ebp as unsigned integer
    if (nbrTry=0) then return
    nbrTry-=1
    'retrieving the pointers
    _esp=Trys(nbrTry).esp
    _eip=Trys(nbrTry).eip
    _ebp=Trys(nbrTry).ebp
    'creating an exception object
    var newException=cptr(Exception ptr,allocate(sizeof(EXCEPTION)))
    newException->TypeName=ExceptionTypeName
    newException->Code=errno
    newException->Message=msg
    newException->ObjData=objData
    'restoring the pointer and hte eip
    asm
        mov eax,[_esp]
        push [_eip]
        pop [eax+4]
        push [_ebp]
        pop [eax]
        mov esp,eax
        'put the exception object pointer to eax as return value
        mov eax,[newException]
        pop ebp
        'now the stack is as before the try... statement, in the select case, the value will be different from zero, so it will run the case else
        ret
    end asm
   
    return
end sub


main.bas

Code: Select all

declare sub mySub()

BeginTry
    print "this is inside my try"
    mySub()
    print "if all is ok"
Catch(myException)
    if (myException->TypeName=ExceptionTypeName) then
        print "an exception occured"
        print myException->Message
    else
        print "unknow exception occured"
    end if
EndTry
sleep

sub mySub()
    print "im inside my sub"
    THROW(1,"this is an exception from my sub",0)
    print "this should not be shown"
end sub
Tourist Trap
Posts: 2901
Joined: Jun 02, 2015 16:24

Re: [Win32] Exception handling using Try/Catch/Finally

Postby Tourist Trap » May 25, 2017 11:33

Cherry wrote:I wonder why nobody is interested in this.

It's very interesting! Just gone unseen with the time!
htm_hx
Posts: 17
Joined: May 24, 2017 19:42

Re: [Win32] Exception handling using Try/Catch/Finally

Postby htm_hx » May 26, 2017 23:45

FB can produce a stable try like VC exception mechanism; the landlord's code, I tested the DLL injection into the process to throw an exception, the process disappeared directly

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 3 guests