fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: try...catch and XS0165 (Use of unassigned local variable...)

try...catch and XS0165 (Use of unassigned local variable...) 9 months 1 week ago #6130

  kitz's Avatar Topic Author kitz Offline Posts: 16
Hi!
This:
LOCAL oSR as StreamReader
TRY
oSR := StreamReader(cFile)
do while....
enddo
CATCH ex...
FINALLY
oSR:Close()
END TRY
creates error XS0165 for variable oSR.
My question is, how to handle this.
Is oSR already created before exceptions arise? The I consider it OK to do it like that and the message is wrong.
If oSR is NOT already created before exceptions occur, then must I use a separate TRY around the creation of oSR?

Please Log in or Create an account to join the conversation.

try...catch and XS0165 (Use of unassigned local variable...) 9 months 1 week ago #6132

  Chris's Avatar Chris Offline Posts: 1149
It is possible that the constructor of StreamReader fails and throws in exception, in which case oSR will never be assigned indeed, I think that's the theory behind this. In order to get rid of it, you could just assign the var to NULL before the TRY construct, although this is a bit like cheating :)

Alternatively, you could use a BEGUN USING instead, which also takes care of the closing/disposing of the object:

BEGIN USING VAR oSR := StreamReader{"..."}
	...
END USING

Chris
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

try...catch and XS0165 (Use of unassigned local variable...) 9 months 1 week ago #6134

  kitz's Avatar Topic Author kitz Offline Posts: 16
Thanks, Chris!

I found another solution:
TRY
oSR := StreamReader(cFile)
try
do while...
enddo
catch...
finally
oSR:Close()
end try
CATCH...
END TRY (note: no FINALLY here, or at least no oSR:Close in it)

BR Kurt

Please Log in or Create an account to join the conversation.

  • Page:
  • 1