Are you confirming that only for a TextOutputStream the check for NIL is not working anymore?
And if yes, what means “won’t work anymore” ? Is it always NIL?
Just to make sure i understand it correctly: The Sample Code taken from the Docs:
Var file As FolderItem = FolderItem.ShowSaveFileDialog("", "MyInfo.txt")
If file <> Nil Then
Var fileStream As TextOutputStream
fileStream = TextOutputStream.Create(file)
fileStream.WriteLine(NameField.Value)
fileStream.WriteLine(AddressField.Value)
fileStream.WriteLine(PhoneField.Value)
fileStream.Close
End If
now should be more like this?:
Var file As FolderItem = FolderItem.ShowSaveFileDialog("", "MyInfo.txt")
If file <> Nil Then
Var fileStream As TextOutputStream
Try
fileStream = TextOutputStream.Create(file)
fileStream.WriteLine(NameField.Value)
fileStream.WriteLine(AddressField.Value)
fileStream.WriteLine(PhoneField.Value)
fileStream.Close
Catch err As IOException
// Do stuff here...
End Try
End If
Wouldn’t you also need to catch NilObjectException? What about OutOfMemoryException?
Switching the Xojo framework from error codes to exceptions would have been a much better if Xojo supported checked exceptions.
Without them its a complete guess on what calls can raise exceptions and what exceptions can be raised. What is even worse is that Xojo could change it between versions and we would never know.
You catch exceptions you know how to handle as their type:
try
var toStream as TextOutputStream = TextOutputStream.Open(tfMyDoc)
toStream.Write(tsData)
toStream.Close
catch ex as NilObjectException
// More than likely the FolderItem
MessageBox("Something was nil.")
catch ex as OutOfMemoryException
MessageBox("Out of memory! " + ex.Message)
end try
If you’re going to do something you shouldn’t like catch all exceptions, you can use Introspection to reach which one it is. The code for that is on the documentation page for Introspection - it’s the main example.
If you’d like to continue this discussion in a new thread, I’d be happy to help answer questions about exceptions
Not in my example. Because we check for file<>nil right before we create the TextOutputStream and i gues NameField, AddressField and PhoneField are TextFields within scope and their Values can’t be Nil. And @Greg_O_Lone wrote:
If you have already checked for <> nil, there should be no way that a NilObjectException can occur, or why have the test?
If testing for nil doesn’t work, that’s a serious bug.
What am I missing here?