简体   繁体   English

异常后执行程序

[英]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. 例如,如果文档中有10行,而第三行有错误,我希望无论如何都要插入9条。 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. 您将所有代码包含在try块中,包括循环。

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; 但是,通常情况下,一个大规模的try块是不可行的,因为那样,您的单个catch块就没有确定错误发生位置的真正方法,并且您没有适当地控制程序的流程-它落入了该块中最终无处可去 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. 如果您使用多个try / catch块(每个需要处理的潜在异常一个),则可以定制每个catch块以处理该特定异常,特别是针对发生异常的位置。 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; 如果由于异常而要退出循环,可以使用break; If you want to skip to the next iteration, you can use continue; 如果您想跳到下一个迭代,则可以使用continue; If you want to get out of the routine altogether, you can use return; 如果您想完全摆脱常规,可以使用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. 生产将更快,更愉快,并且产品将更易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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