PDF (US Ltr)- 1.1Mb
PDF (A4)- 1.1Mb
HTML Download (TGZ)- 0.9Mb
HTML Download (Zip)- 1.0Mb
When writing scripts for MySQL Shell you can often simply rely on the exception handling done by MySQL Shell. For all other languages either proper exception handling is required to catch errors or the traditional error handling pattern needs to be used if the language does not support exceptions.
The default error handling can be changed by creating a customSessionContext
and passing it to themysqlx.getSession()
function. This enables switching from exceptions to result based error checking.
The following example shows how to perform proper error handling. The example assumes that the test schema exists and that the collectionmy_collection
exists.
var mysqlx = require('mysqlx'); var mySession; try { // Connect to server on localhost mySession = mysqlx.getSession( { host: 'localhost', port: 33060, user: 'user', password: 'password' } ); } catch (err) { print('The database session could not be opened: ' + err.message); } try { var myDb = mySession.getSchema('test'); // Use the collection 'my_collection' var myColl = myDb.getCollection('my_collection'); // Find a document var myDoc = myColl.find('name like :param').limit(1) .bind('param','L%').execute(); // Print document print(myDoc.first()); } catch (err) { print('The following error occurred: ' + err.message); } finally { // Close the session in any case mySession.close(); }