From d97fd35b55974de2d6b14ad88239f1848887fd77 Mon Sep 17 00:00:00 2001 From: Mike Taylor Date: Fri, 11 Oct 2002 12:19:09 +0000 Subject: [PATCH] Add the promised sample program with error checking. --- doc/zoom.xml | 82 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 12 deletions(-) diff --git a/doc/zoom.xml b/doc/zoom.xml index cba9e9e..c2778db 100644 --- a/doc/zoom.xml +++ b/doc/zoom.xml @@ -1,5 +1,5 @@ - + ZOOM-C++ @@ -25,11 +25,10 @@ For example, here is a tiny Z39.50 client that fetches and displays the MARC record for Farlow & Brett Surman's - - The Complete Dinosaur + The Complete Dinosaur from the Library of Congress's Z39.50 server: - + #include <iostream> #include <yaz++/zoom.h> @@ -39,14 +38,16 @@ { connection conn("z3950.loc.gov", 7090); conn.option("databaseName", "Voyager"); - resultSet rs(conn, prefixQuery("@attr attr 1=7 0253333490")); + conn.option("preferredRecordSyntax", "USMARC"); + resultSet rs(conn, prefixQuery("@attr 1=7 0253333490")); const record *rec = rs.getRecord(0); cout << rec->render() << endl; } - + - (Note that, for the sake of simplicity, this does not check for - errors: we show a more realistic version of this program later.) + (Note that, for the sake of simplicity, this program does not check + for errors: we show a more robust version of the same program + later.) Yaz++'s implementation of the C++ binding is a thin layer over Yaz's @@ -432,10 +433,10 @@ Usually that's what you want: it means that you can easily fetch a record, use it and forget all about it, like this: - + resultSet rs(conn, query); cout << rs.getRecord(0)->render(); - + But sometimes you want a record to live on past the lifetime of the resultSet from which it was @@ -443,7 +444,7 @@ be used to make an autonomous copy. The application must delete it when it doesn't need it any longer: - + record *rec; { resultSet rs(conn, query); @@ -452,7 +453,7 @@ } cout << rec->render(); delete rec; - + @@ -583,6 +584,63 @@ + + Revised Sample Program + + Now we can revise the sample program from the + introduction + to catch exceptions and report any errors: + + + /* g++ -o zoom-c++-hw zoom-c++-hw.cpp -lyaz++ -lyaz */ + + #include <iostream> + #include <yaz++/zoom.h> + + using namespace ZOOM; + + int main(int argc, char **argv) + { + try { + connection conn("z3950.loc.gov", 7090); + conn.option("databaseName", "Voyager"); + conn.option("preferredRecordSyntax", "USMARC"); + resultSet rs(conn, prefixQuery("@attr 1=7 0253333490")); + const record *rec = rs.getRecord(0); + cout << rec->render() << endl; + } catch (systemException &e) { + cerr << "System error " << + e.errcode() << " (" << e.errmsg() << ")" << endl; + } catch (bib1Exception &e) { + cerr << "BIB-1 error " << + e.errcode() << " (" << e.errmsg() << "): " << e.addinfo() << endl; + } catch (queryException &e) { + cerr << "Query error " << + e.errcode() << " (" << e.errmsg() << "): " << e.addinfo() << endl; + } catch (exception &e) { + cerr << "Error " << + e.errcode() << " (" << e.errmsg() << ")" << endl; + } + } + + + The heart of this program is the same as in the original version, + but it's now wrapped in a try block followed by + several catch blocks which try to give helpful + diagnostics if something goes wrong. + + + The first such block diagnoses system-level errors such as memory + exhaustion or a network connection being broken by a server's + untimely death; the second catches errors at the Z39.50 level, + such as a server's report that it can't provide records in USMARC + syntax; the third is there in case there's something wrong with + the syntax of the query (although in this case it's correct); and + finally, the last catch block is a + belt-and-braces measure to be sure that nothing escapes us. + + + References -- 1.7.10.4