Common uses of exceptions are for occasions where you expect something to work properly but it mysteriously doesn't. For instance, memory allocation. If you're doing lots of memory allocation with a custom routine, you might not want to have to check every returned call for failure. So in the memory alloc code, you put a Try...Catch like so (Note: Highly contrived example)
Try
Lots of different memory allocatinos happening here as objects are made etc
Catch a$
Print a$
End Try
Then in myAllow you would throw if something failed
Function myAlloc(size)
Pointer = allocate some memory here
If pointer = Null then Throw "Out of Memory"
Return pointer
End Function
Other uses are for fileio. Say you're opening a file and traversing it to read (Using your own routines, cause the BlitzMax ones are meant to throw exceptions anyway), say a 3d model format. Now if everytime you read a byte, int, string etc you had to do a check for the return value and then exit or something it would not only be tedious but make your code butt ugly. So you might want to put exception handling code around the model load code, and in your file I/O code, throw an exception.
Try
Open model file
Read stuff
Make objects
Read more
And More
Catch a$
Print a$
End Try
Function myReadByte:Byte()
byte = read byte somehow
If Failed then Throw "Can't read from file"
If End Of File then Throw "Reached end of file"
return byte
End Function
Hope this helps
Aaron
Try
Lots of different memory allocatinos happening here as objects are made etc
Catch a$
Print a$
End Try
Then in myAllow you would throw if something failed
Function myAlloc(size)
Pointer = allocate some memory here
If pointer = Null then Throw "Out of Memory"
Return pointer
End Function
Other uses are for fileio. Say you're opening a file and traversing it to read (Using your own routines, cause the BlitzMax ones are meant to throw exceptions anyway), say a 3d model format. Now if everytime you read a byte, int, string etc you had to do a check for the return value and then exit or something it would not only be tedious but make your code butt ugly. So you might want to put exception handling code around the model load code, and in your file I/O code, throw an exception.
Try
Open model file
Read stuff
Make objects
Read more
And More
Catch a$
Print a$
End Try
Function myReadByte:Byte()
byte = read byte somehow
If Failed then Throw "Can't read from file"
If End Of File then Throw "Reached end of file"
return byte
End Function
Hope this helps
Aaron