Catch?

BlitzMax Forums/BlitzMax Beginners Area/Catch?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I think slowly ;-). When I read the thread I was left with the impression that Try-Catch was only debug mode, which is incorrect. I wanted to make it clear to subsequent readers that it works with release versions as well. But yes, after I posted, I realized that the array explanation had been pointed out by Dreamora. And I agree: more examples are needed, as well as examples of creating custom exception objects. So, here's one:

Type  invalidValueException Extends TBlitzException

	field msg:string = null

	Method toString:String()
		if msg then return msg else return "Invalid value!"
	End Method
End Type


And it's thrown like this:

function strToBool:byte(s:string)
	
	select s.toLower().trim()	
		case "true"
			return True
		case "false"
			return False
		default
			local e:invalidValueException = new invalidValueException
			e.msg = "Bad value passed: " + s
			Throw e 
	end select

end function


and caught like this:

try 
	b = strToBool(s)

catch ex:invalidValueException
	print ex.toString()
	' Do some error recovery things here

catch ex:object		' Catch other exceptions
	print ex.toString()
	' Do some other things here
end try