As3 Error Handling Best Practices
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more error handling best practices c# about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users python error handling best practices Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each ssis error handling best practices other. Join them; it only takes a minute: Sign up How to catch all exceptions in Flex? up vote 43 down vote favorite 17 When I run a Flex application in the debug flash player I get an angularjs error handling best practices exception pop up as soon as something unexpected happened. However when a customer uses the application he does not use the debug flash player. In this case he does not get an exception pop up, but he UI is not working. So for supportability reasons, I would like to catch any exception that can happen anywhere in the Flex UI and present an error message in a Flex internal popup. By using Java I would just encapsulate
Javascript Error Handling Best Practices
the whole UI code in a try/catch block, but with MXML applications in Flex I do not know, where I could perform such a general try/catch. flex exception error-handling share|improve this question asked Sep 19 '08 at 12:43 Yaba 3,08182944 add a comment| 9 Answers 9 active oldest votes up vote 49 down vote accepted There is no way to be notified on uncaught exceptions in Flex 3. Adobe are aware of the problem but I don't know if they plan on creating a workaround. The only solution as it stands is to put try/catch in logical places and make sure you are listening to the ERROR (or FAULT for webservices) event for anything that dispatches them. Edit: Furthermore, it's actually impossible to catch an error thrown from an event handler. I have logged a bug on the Adobe Bug System. Update 2010-01-12: Global error handling is now supported in Flash 10.1 and AIR 2.0 (both in beta), and is achieved by subscribing the UNCAUGHT_ERROR event of LoaderInfo.uncaughtErrorEvents. The following code is taken from the code sample on livedocs: public class UncaughtErrorEventExample extends Sprite { public function UncaughtErrorEventExample() { loaderInfo.uncaughtErrorEvents.addEventListener( UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler); } private function uncaughtErrorHandler(event:UncaughtErrorEvent):void { if (event.error is Error) { var error:Error = event.error as Error; // do something with the error } else if (event.error is ErrorEvent) { var errorEvent:ErrorEvent = event.error as ErrorEvent; // do so
Home ArticlesBest Practices Exceptions and Exception Handling Exceptions and Exception Handling Email to Friend Print Article Remove from Favorites Add to Favorites Remove from 'Articles to Read' Add to 'Articles
Java Error Handling Best Practices
to Read' ByColin Moock | Published May 18, 2007 | Best php error handling best practices Practices , Intermediate | Rating: Unrated Exception Bubbling Copyright © 2004 O'Reilly Media, Inc. All Rights Reserved. Essential sql error handling best practices ActionScript 2.0By Colin MoockJune 2004ISBN: 0-596-00652-7More info... . Available from booksellers or direct from O'Reilly Media, www.oreilly.com. This content is excerpted from the above-named O'Reilly publication, with permission, by agreement http://stackoverflow.com/questions/101532/how-to-catch-all-exceptions-in-flex with ActionScript.org. Earlier we learned that exceptions in a try block can be thrown either directly or as the result of a method call. In reality, an exception can be thrown anywhere in an ActionScript program, even on a frame in a timeline! Given that an exception can be thrown anywhere, how does the ActionScript interpreter find the corresponding catch http://www.actionscript.org/resources/articles/603/8/Exceptions-and-Exception-Handling/Page8.html block to handle it? And what if no catch block exists? These mysteries are resolved through the magic of exception bubbling. Let's follow along a bubbly ride with the ActionScript interpreter as it encounters a throw statement in a program. During the following dramatization, the interpreter's musing are shown in code comments. When a throw statement executes, the interpreter immediately stops normal program flow and looks for an enclosing try block. For example, here's a throw statement: // INTERPRETER: Hmm. A throw statement.// Is there an enclosing try block for it?throw new Error("Something went wrong"); If the throw statement is enclosed in a try block, the interpreter next tries to find a catch block whose parameter's datatype matches the datatype of the value thrown (in the present case, Error): // INTERPRETER: Great, I found a try block. Is there a matching catch block?try {throw new Error("Something went wrong");} If a matching catch block is found, the interpreter transfers program control to that block: try {throw new Error("Something went wrong");// INTERPRETER: Found a catch block whose parameter dataty
contain out of date information.This article is inspired in part by Mike Hadlow's "The first rule of exception handling: do http://www.davidarno.org/2010/12/06/exception-handling-some-thoughts-on-good-practices/ not do exception handling" article that he wrote last year. Whilst it http://www.escapetheory.com.au/blog/tag/best-practices/ is simply the eye-catching title of an otherwise well considered article, to my mind it also encompasses some bad advice often given on the topic. This article is born out of a desire to "set the record straight" and offer up what I see as some exception handling "good error handling practice."
Exception handling is one of those software engineering topics that is simple in theory, but creates a great deal of confusion in practice. It doesn't just confuse either: it also results in some truly awful bad practices. This is often due to people learning exception handling "on the job" whilst using languages with exception handling features that range from error handling best really crap to non-existent. I had the fortune of learning about exceptions within the context of the Ada programming language whilst still at college many years ago. Whilst an old language, exceptions were built into the design of Ada from the start, unlike many languages in use today, which bolted them on as an afterthought. I feel that learning about exception handling so early in my software engineering education has helped me no end in forming a set of exception handling good practices that I (try) to follow: Never go into denial and simply bury troublesome exceptions with a try { ... } catch (Exception e) { // do nothing } 12345678 try{...}catch (Exception e){// do nothing} type piece of code. This is the greatest sin of exception handling. Never just let an exception bubble up to the top of the stack causing the application to exit. Doing absolutely nothing about an exception is the second (if not joint-first) greatest sin of exception handling. Note though that in genuinely exceptional circumstances - eg, you run out ofError #1009: Cannot access a property or method of a null object reference. Error type: Run-time error Error class: TypeError How to Induce error: //in timeline var blart:MovieClip; //this will induce the runtime error 1009 trace(blart.name); stop(); This error is caused when you request the property (example above is 'name') of an object that hasn't been instantiated yet. Note that this same error will occur whether or not the property exists in the object class: //in timeline var blart:MovieClip; //this will induce the same runtime error 1009 trace(blart.FOO); stop(); Solution: ensure that the object is instantiated in run-time: //in timeline var blart:MovieClip = new MovieClip(); trace(blart.name); stop(); Notes: This is a popular error that costs a lot of time in solving. Because the error is so simple to fix, the problem is not always with the immediate code. Check the processes leading up to the instantiation of your object, and create smart checkpoints to make sure your object is created before requesting the required property. If the code is at a point where restructuring the instance methods are difficult, you can work around the error by referencing the object through array notation. The code below would normally trigger the error, but because we use array notation, the error is skipped: //in timeline var blart:MovieClip; //this will NOT induce the runtime error 1009 trace(['blart']['name']); stop(); The workaround is not recommended because your subsequent code will need to account for both valid and invalid object references! Use at your own risk. Share on Facebook Tagged as: ActionScript 3, AS3, as3 error handling, best practices, Error, Error 1009, TypeError, workaround No Comments 5Aug/090 Using the AS3 Debug Console Unlike the examples given in the tutorials and example files, it is very likely that you'll encounter errors at some point of your Flash programming journey. Most of the time, the errors would be straightforward - for example, you have supp