【问题标题】:What Can I use In this Code Instead of try-catch exception handling [closed]我可以在这段代码中使用什么而不是 try-catch 异常处理 [关闭]
【发布时间】:2023-03-27 13:37:01
【问题描述】:

我可以在这段代码中使用什么来代替 try-catch 异常处理? 我尝试使用 Java throws 关键字来声明异常而不是 try-catch 异常处理,但它没有在测试用例中通过。有没有其他方法来处理这里的异常? 我试图删除一些重复的代码,我用 *** 标记了重复的代码(我在 8 个方法中有相同的代码),但是如果我创建一个方法并使用 try catch,它使用的是它自己的 try 块中的类对象块也在那里,然后我必须传递的参数仍在尝试块中,在最后一行我已经注释掉了

AjaxResponseBean moveControl(@PathVariable Integer formId, @RequestBody AjaxRequestBean request) {
        try {
            Form form = formService.getFormWithDraft(formId);
            FormDraftData draft = formService.getFormWithDraft(formId).getDraft();

            String formXml = draft.getData();
            String id = request.getData().getControls().get(0).getId();
            String beforeId = request.getData().getControls().get(0).getBeforeId();

            JAXBContext jaxbControlContext = JAXBContext.newInstance(XmlFormBean.class);
            Unmarshaller jaxbControlUnmarshaller = jaxbControlContext.createUnmarshaller();
            XmlFormBean xmlFormBean = (XmlFormBean) jaxbControlUnmarshaller.unmarshal(new StringReader(formXml));

            XmlFormBean.Control xmlControl = new XmlFormBean.Control(id);
            for (XmlFormBean.Control c : xmlFormBean.getControls()) {
                if (c.getId().equalsIgnoreCase(id)) {
                    xmlControl = c;
                    break;
                }
            }

            xmlFormBean.getControls().remove(xmlControl);
            int index = -1;
            if (StringUtils.isNotBlank(beforeId)) {
                index = xmlFormBean.getControls().indexOf(new XmlFormBean.Control(beforeId));
            }
            if (index == -1) {
                xmlFormBean.getControls().add(xmlControl);
            } else {
                xmlFormBean.getControls().add(index, xmlControl);
            }
//******************************
       Marshaller jaxbControlMarshaller = jaxbControlContext.createMarshaller();
            StringWriter writer = new StringWriter();
            jaxbControlMarshaller.marshal(xmlFormBean, writer);
            form.getDraft().setData(writer.toString());
            formService.addForm(form);

            // AjaxResponseBean
            AjaxResponseBean response = new AjaxResponseBean();
            response.setResult("success");
            response.setData(request.getData());
            return response;


        } catch (JAXBException e) {
            e.printStackTrace();
        }

        AjaxResponseBean response = new AjaxResponseBean();
        AjaxDataBean data = new AjaxDataBean();
        data.setMessage("The client request failed.");
        response.setResult("failure");
        response.setData(data);
        //return MarshallerSomething(form, jaxbControlContext, xmlFormBean, request);
        return response;
//******************************
    }
}

【问题讨论】:

  • 您能否详细说明这里的场景,“但它没有在测试用例中通过”不清楚您要在这里实现什么。您想通过测试用例测试异常场景吗?如果发生异常,您可以通过 try-catch 块捕获该异常,或者使用 throws 在调用方法处回退该异常。您在这里有什么要求?
  • 实际上我试图删除一些重复的代码,我用 *** 标记了重复的代码(我在 8 个方法中有相同的代码)看看,但它使用的是 try 块中的类对象它自己的,如果我创建一个方法并在那里使用 try catch 块,那么我必须传递的参数仍然在 try 块中,在最后一行我已经注释掉了,
  • 如果我理解正确,您需要创建一个新方法并传递一些参数,但这些参数是在 try 块内定义的,您需要从 try-catch 块外部调用新方法。如果是这种情况,您可以在 try 块之外定义参数并在 try 块内初始化它们,以便在 try-catch 块之外也可以使用所需的参数。如果您需要代码,请告诉我,并将在答案块中提供。
  • 我传递的参数 (JAXBContext jaxbContext = JAXBContext.newInstance(XmlFormBean.class);) 需要在 try 块中,否则会显示编译时异常,并且每个方法的 JAXBContext 不同,所以我不能把它放在我正在创建的删除重复代码的方法上。

标签: java jackson jaxb


【解决方案1】:

您可以在params中提及JAXBContext的超类,并根据方法要求传递子类,这样即使需要不同的实现,也可以取出公共代码并移动到不同的方法中。

假设 Context 是 JAXBContext 的超类,而 JContext 是它的子类。

public void testMethod(Context con, String str){
//your logic here
}

Context c1 = new JAXBContext()
Context c2 = new JContext() 
testMethod(c1, "test1")
testMethod(c2, "test2")

你可以试试下面不会得到编译时异常

AjaxResponseBean moveControl(@PathVariable Integer formId, @RequestBody AjaxRequestBean request) {
        Form form = null;
        JAXBContext jaxbControlContext= null;
        XmlFormBean xmlFormBean = null;
            try {
                form = formService.getFormWithDraft(formId);
                FormDraftData draft = formService.getFormWithDraft(formId).getDraft();

                String formXml = draft.getData();
                String id = request.getData().getControls().get(0).getId();
                String beforeId = request.getData().getControls().get(0).getBeforeId();

                jaxbControlContext = JAXBContext.newInstance(XmlFormBean.class);
                Unmarshaller jaxbControlUnmarshaller = jaxbControlContext.createUnmarshaller();
                xmlFormBean = (XmlFormBean) jaxbControlUnmarshaller.unmarshal(new StringReader(formXml));

                XmlFormBean.Control xmlControl = new XmlFormBean.Control(id);
                for (XmlFormBean.Control c : xmlFormBean.getControls()) {
                    if (c.getId().equalsIgnoreCase(id)) {
                        xmlControl = c;
                        break;
                    }
                }

                xmlFormBean.getControls().remove(xmlControl);
                int index = -1;
                if (StringUtils.isNotBlank(beforeId)) {
                    index = xmlFormBean.getControls().indexOf(new XmlFormBean.Control(beforeId));
                }
                if (index == -1) {
                    xmlFormBean.getControls().add(xmlControl);
                } else {
                    xmlFormBean.getControls().add(index, xmlControl);
                }
//******************************
                Marshaller jaxbControlMarshaller = jaxbControlContext.createMarshaller();
                StringWriter writer = new StringWriter();
                jaxbControlMarshaller.marshal(xmlFormBean, writer);
                form.getDraft().setData(writer.toString());
                formService.addForm(form);

                // AjaxResponseBean
                AjaxResponseBean response = new AjaxResponseBean();
                response.setResult("success");
                response.setData(request.getData());
                return response;


            } catch (JAXBException e) {
                e.printStackTrace();
            }

            AjaxResponseBean response = new AjaxResponseBean();
            AjaxDataBean data = new AjaxDataBean();
            data.setMessage("The client request failed.");
            response.setResult("failure");
            response.setData(data);
            return MarshallerSomething(form, jaxbControlContext, xmlFormBean, request);
            //return response;
//******************************
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2020-05-18
    相关资源
    最近更新 更多