简体   繁体   中英

Executing program after exception

Hello I want to execute a program to insert from a text document into a database. Right now I am just finishing up the errors part. At the moment it enters the information into the database but when it gets an error in a data field, the program stops its execution.

My objective is that it will log the error and continue the execution of the program. For example if there are 10 lines in the document and let's say the third has an error, I want 9 inserts to go through regardless. At the moment in this example my code will just insert the first two lines. Is there a method or something similar that will help me accomplish this?

**editted formating

    try {

        while (true) {

            String line = reader.readLine();
            if (line == null) {

                break;
            }
            String[] parts = line.split("");
            for (String part : parts) {
                array1.add(part);
            }
            String query = " insert into FRONTMC.HECHO (folio_hecho, folio_orden, emisora, serie,"
                    + "clave_sentido, titulos_hecho, precio, importe, liquidacion, contraparte, id_estatus, isin, contrato,"
                    + "secondary_exec_id, exec_id, F11_ClOrdID, fecha_recepcion, fecha_sentra)"
                    + " values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,convert(varchar(30),cast(? as datetime),120),convert(varchar(30),cast(? as datetime),120))";

            PreparedStatement preparedStmt = con.prepareStatement(query);

            for (int counter = 0; counter < array1.size(); counter++) {
                if (array1.get(counter).substring(0, 3).equals("37=")) {
                    preparedStmt.setString(1, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 3).equals("37=")) {
                    preparedStmt.setString(2, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 3).equals("49=")) {
                    preparedStmt.setString(3, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 4).equals("447=")) {
                    preparedStmt.setString(4, array1.get(counter).substring(4));
                }
                if (array1.get(counter).substring(0, 3).equals("54=")) {
                    preparedStmt.setString(5, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 3).equals("32=")) {
                    preparedStmt.setString(6, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 3).equals("31=")) {
                    preparedStmt.setString(7, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 4).equals("381=")) {
                    preparedStmt.setString(8, array1.get(counter).substring(4));
                }
                if (array1.get(counter).substring(0, 3).equals("63=")) {
                    preparedStmt.setString(9, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 4).equals("448=")) {
                    preparedStmt.setString(10, array1.get(counter).substring(4));
                }
                if (array1.get(counter).substring(0, 4).equals("150=")) {
                    preparedStmt.setString(11, array1.get(counter).substring(4));
                }
                if (array1.get(counter).substring(0, 3).equals("48=")) {
                    preparedStmt.setString(12, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 2).equals("1=")) {
                    preparedStmt.setString(13, array1.get(counter).substring(2));
                }
                if (array1.get(counter).substring(0, 4).equals("527=")) {
                    preparedStmt.setString(14, array1.get(counter).substring(4));
                }
                if (array1.get(counter).substring(0, 3).equals("17=")) {
                    preparedStmt.setString(15, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 3).equals("11=")) {
                    preparedStmt.setString(16, array1.get(counter).substring(3));
                }
                if (array1.get(counter).substring(0, 3).equals("52=")) {
                    String date = array1.get(counter).substring(3);
                    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
                    SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
                    String ds2 = sdf2.format(sdf1.parse(date));
                    String x = date.substring(9, 21);
                    String newfecha1 = ds2 + " " + x;
                    preparedStmt.setString(17, newfecha1);
                }
                if (array1.get(counter).substring(0, 3).equals("52=")) {
                    String date = array1.get(counter).substring(3);
                    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
                    SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
                    String ds2 = sdf2.format(sdf1.parse(date));
                    String x = date.substring(9, 21);
                    String newfecha1 = ds2 + " " + x;
                    preparedStmt.setString(18, newfecha1);
                }
            }
            preparedStmt.execute();
        }
        System.out.println(array1);
        System.out.println("insert complete");
        new Thread(new Runnable() {
            @Override
            public void run() {
                exitomsj();
            }
        }).start();
        reader.close();
    } catch (Exception tryerror) {
        System.out.println("Error en dato");
        tryerror.printStackTrace();
    }

Looking past the major need for some cleaning, your problem is controlling the flow of the program, or, more specifically the lack thereof. You have all your code inside a try block, including the loop.

Sometimes this is okay, for example if the same exception can occur in multiple places, or multiple exceptions can occur throughout, and you want to handle them in the same way such as throwing your own custom exception or just returning a specific "failed" value.

Usually, however, one massive try block is not okay, because then your single catch block has no real way of determining where the error occurred, and you're not adequately controlling the flow of the program - it falls into this one block at the end with no where to go afterwards; the routine you want to continue is behind it.

If you employ multiple try/catch blocks, one for each potential exception that needs to be handled, you can tailor each catch block to deal with that specific exception, specifically for where it occurred. You then don't need to worry about trying to "discover" what happened, or where, and you have not lost your position in the program's execution, meaning you can decide what you want to do with it.

If you want to exit a loop because of an exception, you can use break; If you want to skip to the next iteration, you can use continue; If you want to get out of the routine altogether, you can use return;

If you have multiple nested loops, you can specify which loops you want to break or continue by labeling them. Eg:

mainLoop:
for (int i = 0; i != someVariable; i++) {
    //do something
    nestedLoop:
    for (int ii = 0; ii != someOtherVariable; ii++) {
        //some code...
        /*
        ** Here, you can then skip the remainder of the "nestedLoop" block,
        ** and start the next iteration of the "mainLoop" using `continue mainLoop;`
        */
         //some more code...
    }
}

I strongly recommend working on the presentation of your code, and utilising methods to do something based on the given parameters. That will drastically shorten the amount of code you actually have to write. Production will be faster, more enjoyable, and the product will be easier to maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM