Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Debugging, exceptions and error handling

Smart has full support for ordinary Object Pascal exceptions, in other words the same system used by both Delphi and free-pascal.

What is an exception?

An exception is simply a signal that something has gone wrong. Under object pascal all exceptions are represented by an exception object, which contains information about the error that has occurred. The most basic exception object simply contains a text string called message, which contains a string representation of the error that occurred. Here is how you raise an exception:

  if (something<>whatyouexpect) then
  raise exception.create('I expected something else');

The exception object is an object like everything else. It is normal to create your own exception objects if your error needs more information than just a string.

Catching exceptions

Catching an exception is done inside a try / except block. If the code inside the block raises an exception then you can safely handle it before either exiting your method or continuing execution:

try
  DoSomethingThatCanFail;
except
  on e: exception do
  w3_showmessage('The following error occured:' + e.message);
end;

The above code is quite generic. It will catch all errors of type exception (which is all of them). The exception object is made available as the “e” variable. In the above code we display the message (string text) field of the exception.

Being more selective

While catching all types of exception with the same error handler is very effective, there are times when you want to handle specific errors differently. Object Pascal allows you to handle that in the following way:

try
  DoSomethingThatCanFail;
except
  on e: EW3FirstErrorType do
  w3_showmessage('A EW3FirstErrorType error occured:' + e.message);

  on e: EW3SomeOtherErrorType do
  w3_showmessage('A different error occured:' + e.message);

  on e: exception do
  w3_showmessage('All other errors are handled by this one');
end;
Ignoring exceptions

Unless you handle an exception it causes the execution of the current procedure (recursively back to the first caller) to be aborted. In other words, it will stop running your code and propagate the error upwards. So if you don’t handle errors, it will report it to the method that called your code, or exit that and continue until it finds an exception handler. If you simply want to ignore any errors and continue no matter what (not recommended) you can do as such:

try
  DoSomethingThatCanFail;
except
  on e: exception do; //Ignore any errors and continue
end;

Debugging

Since Smart generates javascript designed to run in browsers, the best way to debug is to use a browser. As of writing Smart target’s iOS exclusively (meaning: iPhone and iPad on Safari mobile) and as such the best way to debug is to install and use Safari. Safari has one of the best debuggers built into it.

  • Start by downloading and installing Safari on your PC or virtual machine.
  • When running Safari for the first time, make sure you set Safari as your main browser
  • Go to: Edit -> Options -> Advanced and enable “display developer menu”
Safari has a very good debugger built into it

Safari has a very good debugger built into it

To make Smart Mobile Studio use safari rather than it’s built in Chrome instance, do the following:

  • In Smart Mobile Studio click on the preferences button, then go to the server tab and disable “use built-in browser to display my apps”. This will force Smart to run your applications in Safari
  • When you run your app, go to the safari developer menu and click “show web inspector”

It is also important that you enable debugging info in your project option (Options -> Compiler tab). And of-course it’s equally important that you use try/except sections in your code.

Make sure your project options include debug information, otherwise your exception handling might not make sense

Make sure your project options include debug information, otherwise your exception handling might not make sense

External reference
  • Delphi Basics, exception handling

Leave a comment Cancel reply

You must be logged in to post a comment.

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS