我可以在此代码中使用什么来代替 try-catch 异常处理
What Can I use In this Code Instead of try-catch exception handling
我可以在此代码中使用什么来代替 try-catch 异常处理?
我尝试使用 Java throws 关键字来声明异常而不是 try-catch 异常处理,但它没有在测试用例中传递。还有其他方法可以处理这里的异常吗?
我试图删除一些重复的代码,我用 *** 标记了重复的代码(我在 8 个方法中有相同的代码),但是如果我创建一个方法,它正在使用它自己的 try 块中的 class 对象并在那里使用 try catch 块然后我必须传递的参数仍在 try 块中,在最后一行我评论说 out
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;
//******************************
}
}
可以在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;
//******************************
}
我可以在此代码中使用什么来代替 try-catch 异常处理? 我尝试使用 Java throws 关键字来声明异常而不是 try-catch 异常处理,但它没有在测试用例中传递。还有其他方法可以处理这里的异常吗? 我试图删除一些重复的代码,我用 *** 标记了重复的代码(我在 8 个方法中有相同的代码),但是如果我创建一个方法,它正在使用它自己的 try 块中的 class 对象并在那里使用 try catch 块然后我必须传递的参数仍在 try 块中,在最后一行我评论说 out
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;
//******************************
}
}
可以在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;
//******************************
}