Throw Catch Try - Cany anyoen give an example ?
BlitzMax Forums/BlitzMax Beginners Area/Throw Catch Try - Cany anyoen give an example ?
| ||
I had never heard of these techniques before and the docs dont really explain them too well. Can anyone give a real example of what these would be used for ? |
| ||
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 |
| ||
Exception handling.Try Repeat a:+1 DrawText (a+"is OK",400,300) If a>5 Throw "chunks" Flip Cls Delay 100 Forever Catch a$ cls DrawText ("caught exception "+a$,400,300) flip waitmouse EndTry essentially it is in the format of: try > throw > catch > end try the try / end try block is also referred to as an Exception Block, you are trying to isolate a behavior throw forwards a caught exception catch handles the exception: here is another example that is a little more useful: Rem Throw generates a BlitzMax exception. End Rem Graphics 800,600,0 Local bmax:String[]=["one","two","three","four"] Try Repeat a:+1 DrawText (bmax[a],400,300) If a > 4 Then Throw "BAM!" Flip Cls Delay 300 Forever Catch ex:Object cls DrawText (ex.Tostring() ,10,300) flip waitmouse EndTry |
| ||
It can lead to sloppy code so I would only use it where I felt I didn't have a choice but to... |
| ||
It CAN but then again it can also lead to much easier to read code. |
| ||
EDIT : opinion removed...1. *********************** 2. Try / Throw / catch blocks are useful in many instances as are many other debugging techniques, and they don't necessarily lead to sloppy code. 3. You can also use Assert to verify value ranges. |
| ||
Though it's obvious to people who've used Try/Catch before, the two biggest benefits in my opinion is that you can throw objects and the error can be many levels deep. The examples above don't really should this so... Type TXmlReaderException Function Create:TXmlReaderException(ExceptionString:String) Local XmlReaderException:TXmlReaderException=New TXmlReaderException XmlReaderException.ExceptionString=ExceptionString Return XmlReaderException End Function Method toString:String() Return "XmlReaderException: " + ExceptionString End Method Field ExceptionString:String End Type Type TXmlReader Method Read() PartOfReader() End Method Method PartOfReader() AnotherPart() End Method Method AnotherPart() EvenMore() End Method Method EvenMore() SomethingGoesWrongInThisPartOfReading() End Method Method SomethingGoesWrongInThisPartOfReading() Throw TXmlReaderException.Create("Closing Tag not found on line...") End Method End Type Try XmlReader:TXmlReader=New TXmlReader XmlReader.Read() Catch XmlReaderException:TXmlReaderException Print XmlReaderException.toString() End Try Also, in the catch if it's a minor error you can handle it gracefully (doesn't force your program to quit) or at the very least you can quit gracefully. |
| ||
bradford, skunk is entitled to his opinion and is entitled to share it. There was nothing wrong with his post up there that I could tell. ... After all those examples I still don't see the point of using this. It seems to just save you from using 1 temp variable. |
| ||
This helps alot, thanks :) Perhaps the doc maker would be clever and add an example like this. |