Exception handling

The Java SDK contains the TenTenException class.

The TenTenException class can be caught with try/catch. The TenTenException class is the parent class of all of the 1010data Java SDK exceptions. The following example uses try/catch for exception handling:

import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.io.FileWriter;
import com.tentendata.javasdk1010v2.*;

public class RowWriter {
    public static void writerows(ResultSet results, Writer out)
    throws IOException {
        for (long i = 0; i < results.numRows(); i++) {
            Row row = results.row(i);
            Datum[] data = row.asArray();
            for (int j = 0; j < data.length; j++) {
                if (j > 0) {
                    out.write('\t');
                }
                out.write(data[j].toString());
            }
            out.write('\n');
        }
    }

    public static void main(String[] args) throws IOException {
        String url = "https://www2.1010data.com/cgi-bin/gw";
        String tablename = "pub.demo.weather.stations";
        String outfilename = "RowOutput.txt";
        String user;
        String pwd;
        String group;
        Session session = null;
        Query query;

        if (args.length < 2) {
            System.err.println("usage: RowWriter USERNAME PASSWORD");
            System.exit(1);
        }
        user = args[0];
        pwd = args[1];
        System.out.println("Single-user login for user " + user);
        try {
            session = new Session(url, user, pwd, LoginType.POSSESS);

            System.out.println("Session established.");
            query = new Query(session, tablename,
                              "<sel value=\"(elev>2)\"/>");
            ResultSet results = query.run();
            System.out.println("Query run.");
            FileWriter output = new FileWriter(outfilename);
            try {
                writerows(results, output);
            }
            finally {
                output.close();
            }
        }
        catch (TenTenException e) {
            System.err.println("1010 Error: " + e);
        }
        finally {
            if (session!=null) {
                session.close();
            }
        }
        System.out.println("Done.");
    }
}