Try, catch

Hi !

I have trouble understanding how Try,Catch works.

Try
dim s as string
dim rs As RecordSet
rs=db.SQLSelect(“select * from DATEINVENTAIRE”)
// database table above does not exist
s=rs.IdxField(1).Value
Catch e As NilObjectException
MsgBox “error”
End Try

Debugger stops with NilObjectException at line :

s=rs.IdxField(1).Value

So catch does not works…but i don’t know what the problem is.

Can someone help me ?

Are you debugging?
If so, you must continue.

1 Like

Or turn off Break On Exceptions in the Project menu.
Or use the BreakOnExceptions Pragma:

#Pragma BreakOnExceptions Off
Try
  // your code
Catch e As NilObjectException
  // handle exception
End Try
#Pragma BreakOnExceptions Default // Restore setting from Project menu
1 Like

Perhaps your RS = Nil (is your query correct?)

Check the debugger. If break on exceptions is on it will break wen an exception is occoured. When that happens, you can see in the debbuger what is Nil (when you have catched a NilObiectException).

If ra is not nil, click in the debugger on the link right side of rs to see it’s content

1 Like

The debugger is stopping where the error occurred. By continuing to the next line, as @Ramon_SASTRE mentioned, you will drop into the Catch clause.

By using the Pragmas that @Anthony_G_Cyphers suggested, you won’t break into the debugger on that error at all.

Without the Catch, your app would report the error to the user through a dialog and quit.

2 Likes

Many thanks Anthony,

It works with #Pragma, I understand better now.

Just one question, #Pragma instruction is ignored when App is build ? Or may I remove it before build ?

It will have no effect when compiled, it is a debugger directive.

1 Like

However, in your specific example, you should check the db error instead of trapping the exception.

1 Like

Yes Kem, it was just an example, my goal was to understand how Try,Catch works.

2 Likes