If I have a table with an integer field and via script I fill it with a string value in a try…catch block the exception is not triggered.
The same if I fill with NULL a NOT NULL field.
How can I handle these kinds of exceptions?
If I have a table with an integer field and via script I fill it with a string value in a try…catch block the exception is not triggered.
The same if I fill with NULL a NOT NULL field.
How can I handle these kinds of exceptions?
A solution has an “onError” property to which you can attach a global method. That method will then be triggered when exceptions occur.
Take a look at the sample code of “ServoyException” to see an example onError-method.
At solution level ?!
I prefer a form level event…
But what’s the use of try…catch ? What kind of exception is triggered?
I explain better my problem:
I have a function that import records from a CSV file.
for (row in file..){
err = 0
try{
controller.newRecord()
col_INT= row[1]
col_STR = row[2]
} catch(e){
err = 1
databaseManager.rollbackEditedRecords()
}
if (err){
txt_err += row
} else {
databaseManager.saveData()
}
}
If row[1] is a string and col_INT is an INTEGER field the method trigger the globals.on_error() method instead of catch block.
The problem is that on_error() break the cycle, instead I want concatenate the list of line that generate an error in a txt for display them later in dialog.
up, no one?!
120 Views and no one reply.
The question is simple: how do I handle this exception without brake my function?
column_INT = "aaaaa"
axterics:
120 Views and no one reply.
The question is simple: how do I handle this exception without brake my function?column_INT = "aaaaa"
Hi axterics,
Use the java.lang.Integer.parseInt() method to get the number from the value, If the value will be a string, It will throw a NumberFormatException and that exception will be caught by the catch block.
var val = plugins.dialogs.showInputDialog("Input","Please enter a Number : ");
try{
var num = java.lang.Integer.parseInt(val);
plugins.dialogs.showInfoDialog("","Number","Ok");
}catch(e)
{
plugins.dialogs.showInfoDialog("","String","Ok");
}
java.lang.Long.parseLong(), java.lang.Double.parseDouble() can also be used…
Hope it helps.
It help but I have to made mutiple check(isnull(), isInt(), etc) on moltiple fields.
Servoy already catch the exception but call the globals.on_error() method. There’s no way to catch the catch{ } block instead?
axterics:
It help but I have to made mutiple check(isnull(), isInt(), etc) on moltiple fields.Servoy already catch the exception but call the globals.on_error() method. There’s no way to catch the catch{ } block instead?
Yes, you can check these validations using the classes of java.lang package, which can be available within Servoy. Please, browse JDK 23 Documentation - Home. In any such validation, if the it fails for any Exception(Code should in try block), It will be caught by the catch block, no need to use the globals.on_error() method, if you don’t need it.
You don’t have to use Java code here to get this done:
for (row in file..){
err = 0
try{
controller.newRecord()
col_INT= Math.parseInt(row[1])
col_STR = row[2]
} catch(e){
err = 1
databaseManager.rollbackEditedRecords()
}
if (err){
txt_err += row
} else {
databaseManager.saveData()
}
}
Paul
I perfectly understand this: try…catch trigger only java code exception.
The problem is that if I have 100 fields for record and I have to validate everyone I will become crazy (check not null field, check is not int, check is string length less then x,… etc)
Servoy don’t really need this because it knows something fail (in fact it trigger globals.on_error). I simply ask if there’s no way to handle this in another way.
Try…catch does not only catch Java exceptions, also JavaScript exceptions.
This exception raised automatically by Servoy and currently not catchable in scripting unfortunately.
I have created a case for this so our engineers can have a look if it’s possible to make this possible in a future release.
Regards,
Paul
pbakker:
I have created a case for this so our engineers can have a look if it’s possible to make this possible in a future release.
Thanks a lot. For now I decide to use transaction and call a rollback in globals.on_error()
This is opinion, so feel free to disagree…but, for me, I feel I’m failing to program well if I have to use try blocks.
Exceptions are intended to let you capture things that are out of your control as a programmer. ie, hardware failures, network disconnects, etc.
Also, relying on a language to handle your type conversions implicitly is somewhat cagey…you may get some cases that don’t fail, but give you unexpected results.
Personally, I would encourage you to handle the type anomalies in your code as suggested above.
Just my $0.02.
greg.