Tag: exception

如何通过DocumentBuidlerFactory创建新的xml文件时生成ParserConfigurationException

我将从字符串创建XML。 看起来像 import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; public Document createCompleteExportXml(String xmlFilename, String content) { try { DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder(); //create the XML file here } catch (ParserConfigurationException pce) { LOGGER.trace(“parsing error “, pce); } } 现在我必须测试是否可以在Junit测试中捕获exception。 @Test(expected=ParserConfigurationException.class) public void createCompleteExportXmlWithParseConfigurationException() { String xmlFilename = “junitExportTestWithParseConfigurationException.xml”; String content = “any […]

在Java中拯救吞噬的exception

一些第三方图书馆吞下了一个例外: String getAnswer(){ try{ // do stuff, modify instance state, maybe throw some exceptions // … return computeAnswer(); }catch (SomeException e){ return null; } } 我想把它改成: String getAnswer() throws SomeException{ // do stuff, modify instance state, maybe throw some exceptions // … return computeAnswer(); } 我不能,因为库已经打包成一个jar子。 那么,有没有办法将exception带回来? 我不需要重新抛出,带有exception的堆栈跟踪和消息也可以工作。 我认为反思在这里没有帮助,也许Unsafe ? 是的我知道我可以使用调试器来查明发生了什么,但如果我在运行时需要exception以进行日志记录和那样的东西,这将不会非常有用

使用JMAP获取heapdump时出现exception

当我使用heapdump时,我得到以下exception jmap -F -dump:format = b,file = / tmp / heapdump / before.hprof 10737 Attaching to process ID 10737, please wait… Exception in thread “main” java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at sun.tools.jmap.JMap.runTool(JMap.java:179) at sun.tools.jmap.JMap.main(JMap.java:110) Caused by: java.lang.RuntimeException: Type “nmethodBucket*”, referenced in VMStructs::localHotSpotVMStructs in the remote VM, was not present in […]

在Java中初始化最终变量的问题

我一直在努力解决Java中的一个问题并开始找到我,我无法想到一个合适的方法来解决它。 我有一个最终的对象属性,但是动态。 也就是说,我希望值在指定后保持不变,但每个运行时的值可以不同。 所以我在课程开头声明了类级别变量 – 比如private final FILE_NAME; 。 然后,在构造函数中,我为它赋值 – 比如说FILE_NAME = buildFileName(); 当我在buildFileName()方法中抛出exception的代码时,问题就开始了。 所以我在构造函数中尝试这样的东西: try{ FILE_NAME = buildFileName(); } catch(Exception e){ … System.exit(1); } 现在我有一个错误 – “空白的最终字段FILE_NAME可能尚未初始化。” 这是我开始对Java严格的编译器感到有些恼火的地方。 我知道这不会是一个问题,因为如果它到达catch,程序将退出…但编译器不知道,因此不允许此代码。 如果我尝试在catch中添加一个虚拟赋值,我会得到 – “可能已经分配了最终字段FILE_NAME。” 我显然不能在try-catch之前分配一个默认值,因为我只能分配一次。 有任何想法吗…?

非法监控状态exception

如何将轮询线程传递给另一个线程进行处理。 程序执行在一个控制器类中,它有一个main方法和一个线程池: 主类控制器 public static void main(String[] args) throws InterruptedException { RunnableController controller = new RunnableController(); System.out.println(incomingQueue.size()); controller.initializeDb(); controller.initialiseThreads(); System.out.println(“Polling”); controller.initialUpdate(); } 具有轮询类的线程的方法 private void initialiseThreads() { try { threadExecutorRead = Executors.newFixedThreadPool(10); PollingSynchronizer reader = new PollingSynchronizer(incomingQueue,dbConnection); threadExecutorRead.submit(reader); }catch (Exception e){ e.printStackTrace(); } } 具有proccesor类的线程的方法 private void initialUpdate() { RunnableController.outgoingQueue = incomingQueue; if((RunnableController.outgoingQueue)!= null){ try […]

在struts xml中声明/处理哪些exception

Java中有Checked和Unchecked exceptions 。 由于我是Struts2新手,因此我对处理已checked exception有一个愚蠢的怀疑。 我们编写try catch block来处理已checked exceptions 。 因此,我们可以在catch block编写return语句并return view显示Exception / Error的页面return view名称。 如果我抛出在struts.xml文件中声明的checked exception并且结果是重定向到Error / Exception页面怎么办? 这两种方法都是检查exception的正确方法吗?

FopFactory.newInstance()时Fopexception

我正在使用struts 2,我正在尝试使用fop从xml和xsl创建一个pdf文件。 我在这两个url中开发我的代码http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2PDF.java?view=markup和http://justcode.wordpress的.com / 2009/01/16 / generare-PDF-CON-struts2的-FOP-XML-E-XSLT / 这是我的代码,我正在尝试使用两种不同的方式 public class JspToPdfTest extends ActionSupport{ private InputStream inputStream; private Xml2PdfManager xml2PdfManager; public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public JspToPdfTest(){ xml2PdfManager = new Xml2PdfManager(); } public String doPrint() { String xml = “C:\\Users\\Administrator\\workspace\\path\\actions\\forms\\testeFOPxml.xml”; String xslPath = “C:\\Users\\Administrator\\workspace\\path\\actions\\forms\\xml2fo.xsl”; […]

使用’if’语句检查后出现空指针exception

我收到一个非常烦人的错误,说我得到一个空指针exception但是有一个if语句在继续之前检查文本是否为null: public String[] getFileData() throws IOException { String file_name = “C:/Users/Liloka/Source/textfiles/Lines.txt”; try { ReadFile file = new ReadFile(file_name); aryLines = file.OpenFile(); for(int i =0; i<aryLines.length; i++) { System.out.println(aryLines[i]); } } catch(IOException e) { System.out.println(e.getMessage()); } return aryLines; } public void actionPerformed(ActionEvent evt) { if(evt.getSource() == enterBtn) { String Text = textToAdd.getText(); if(!(Text.equals(null))) { RF.addNewElement(Text); System.out.println(Text); […]

ConcurrentModificationException的

我正在使用下面的代码片段,尝试处理电子表格,需要排除ad hoc列。 我知道我正在做的粗略方式,将exception放在ArrayList中并处理每个列表中的列表并且在当前行列上的增量是不正常的,但是你知道只是完成它。 但是我得到了标题错误,我认为这应该永远不会发生。 我只是循环遍历ArrayList并进行比较,而不是修改任何东西。 错误在哪里? 有没有更好的方法来处理例外列表? ArrayList noProcess = new ArrayList(); Iterator itr00 = noProcess.iterator(); Iterator itr01 = noProcess.iterator(); noProcess.add(new Integer(“5”)); noProcess.add(new Integer(“18”)); …. boolean include=true; for(int i=0;i<archive.length;i++){ for (int j = 0; j < archive[i].length; j++) { while (itr00.hasNext()) { if (j == ( (Integer) itr00.next()).intValue()) include = false; } if (include) {…

Struts 2和Hibernate中的exception处理

假设我们已经在Struts2,Hibernate,MySQL中开发了一个网站,我们在这里添加了一些try/catch块,它们通过Hibernate包含数据库调用。 我的问题是 在catch块内,我正在向记录器发送适当的消息。 在这里,我们不能使用System.out.println作为其网页,还可以做些什么来提醒用户有关exception的信息? 作为测试的一部分,我更改了hibernate.cfg.xml并输入了错误的数据库密码,以便模拟数据库崩溃场景。 正如我所料,它引发了错误 javax.servlet.ServletException: Filter execution threw an exception java.lang.NoClassDefFoundError: com_cenqua_clover/CoverageRecorder my.com.employee.(employee.java:29) com.action.employeeAction.(employeeAction.java:23) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) java.lang.reflect.Constructor.newInstance(Constructor.java:513) java.lang.Class.newInstance0(Class.java:355) java.lang.Class.newInstance(Class.java:308) com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:119) com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:150) com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:139) com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:109) com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:288) com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388) com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187) org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61) org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39) com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47) org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478) org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.20 logs. 从用户的角度来看,这是不可取的,所以如何解决这些问题。 我正在使用Eclipse Juno,Windows XP,MySQL […]