« [tip] use fields without a dataprovider attached | Main | [Tip] Validate email addresses »
October 14, 2005
[tip] 'Try' your statements and 'Catch' an exception
by Marcel Trapman
www.it2be.com
While coding my plugins in Java I became familiar with marking blocks of statements to try and to specify the responce when an exeption is thrown. In other words what happens when an error occurs. Will your code stop executing or is there a nice way out?
The basic code for ‘the nice way out’ looks like this:
try {
//execute your code
} catch(e) {
//output e (=error message)
}
And, when you want to create you code really perfect you can add the finally block also
} finally {
//finish what you were doing
}
The use of this is GREAT but let me illustrate this with an example based on 2 of my plugins.
I was testing my new plugin it2be_data that will import and export all types of datafiles like csv, xml and dbf and execute updates on (external) database sources.
The test was exporting csv data in an end-user friendly way displaying a splash screen to tell him/her to be patient. To archieve this I coded the following:
1. select a filename to write the data to
2. test the filename
3. show a splash to the user telling him/her to be patient
4. create the output array
5. write the file
6. hide the splash
While playing with methods in the plugin I created an error and the error caused the splash to remain in front of the user interface because the code simply stops executing after the error.
In the below code you see I added the try/catch/finally blocks and now the cought error will output the error message and the fininally block will finish. In this case it will hide the splash!
Great stuff huh,
Have fun with it!
//Write a csv file based on a filename, 2 dimensional Array and optional overwrite file parameter...
//Use a query
//Get a dataset based on query on the (crm) example database
var query = "SELECT company_name,company_type,accountmanager FROM companies";
var dataset = databaseManager.getDataSetByQuery("crm", query, null, 1000);
if (!dataset) { //go back when the dataset is empty
plugins.dialogs.showInfoDialog("Attention...", "There are no records!", "OK");
return;
}
//set the name of the file
var filename = plugins.file.showFileSaveDialog();
if (filename) { //when the filename is not empty
//set the wait message for the client
var message = "This could take some time!<br><b>Time for some coffee or so</b> “;
plugins.it2be_splash.showText(message, "#FFFFFF", 3600, false, 5);
//create a new array
var selection = new Array();
//create the first 'row' of headers
selection[0] = new Array("company","type","accountmanager");
//create the data 'rows'
for (var i = 1 ; i <= dataset.getMaxRowIndex() ; i++) {
var row = dataset.getRowAsArray(i);
selection[i] = row;
}
try {
//write the file
plugins.it2be_data.csv().write(filename, selection);
} catch(e) {
//output the error message to the console
application.output(e);
} finally {
//hide the wait message
plugins.it2be_splash.hide();
}
}
| Posted by IT2Be on October 14, 2005 at 04:33 PM in Tips | Permalink