You can put code in this event handler to display a message to the user, capture log information or anything else you want. The event has one parameter, error, which is the exception itself. There is a special event handler in the App object of your project that is called if an unhandled exception is not caught using Try.Catch or Exception. ShowOpenFileDialog( "")Īlthough this is simpler, it is not as obvious what is occurring (especially if there is even more code in the method) and it is also far less flexible since it only works on the entire method. The simplest example of how you can get a NilObjectException is forgetting to use New to get an instance before you try to use a property or call a method on it: If your app handles the exception then the exception handler runs and your code continues. When you Resume, the above dialog will be displayed if your app does not handle the exception. If you are running your app in debug mode from within the Xojo IDE, then first the Debugger appears showing you the line of code that caused the exception and you can see the exception in the Variables pane of the Debugger. If a NilObjectException occurs in your built app, then a dialog appears: This exception occurs when you attempt to use an object but don’t have an instance of it. The most common type of exception that occurs is a NilObjectException. And since that is not something unexpected, relying on the KeyNotFoundException is unnecessary. So if you do expect situations where a key may not be found then you can account for it with these methods. But you can avoid that by using either the Dictionary.Lookup (to apply a default if the key is not found) or the Dictionary.HasKey methods. If the key is not found you'll get a KeyNotFoundException. For example, with a Dictionary you use the Value method to fetch a value based on a key. One reason is that when testing, the Debugger is displayed for any event that is raised if you have "Break On Exceptions" enabled in the Project menu. Since exceptions are for unexpected behavior, if you are able to avoid an error by preventing the exception from occurring then it is more efficient to do so. To do this, select Project ↠ Break On Exceptions in the menu so that it has a checkmark next to it. When an exception is encountered in your code, you can choose to have the Debugger displayed at the line causing the exception. The act of causing an exception to occur is called raising an exception.Īll exception are subclasses of the RuntimeException class. These errors will crash your application if you do not handle them in some way. Once you’ve found the source of errors, you want to make sure you handle them properly.Įxceptions are a type of error that occur when something unexpected happens. The debugger can help you verify that your code is working as you expect and it can help you find errors.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |