2016-01-13 200 views
-3

我给int dmcdw值'd',并希望通过异常捕获错误,但它没有工作。捕捉int异常

这里是我的代码的一部分:

private int dmcdw = d; 
private String cdw = "w"; 

private void cdwPlausi() 
{ 
    try 
    { 

     if (dmcdw > 0^cdw.substring(0).equalsIgnoreCase("w")) 
     { 
      // 
     } 
     else 
     { 
      // 
     } 
    } 
    catch (NumberFormatException ex) 
    { 
     // 
    } 
} 

我使用NumberFormatException的,但它不是为我工作,我究竟做错了什么?

这里是错误消息,在我的控制台显示出来:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 

d cannot be resolved to a variable 

at importiert.Importiert_Tarif.<init>(Importiert_Tarif.java:19) 
at frame.Frame_Main$2.actionPerformed(Frame_Main.java:228) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$500(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 
+0

的可能的复制[Java的:良好的方式来封装的Integer.parseInt()](http://stackoverflow.com/questions/1486077/java-good -way-to-encapsulate-integer-parseint) – amkz

+1

你是什么意思*“它不工作”*? – Andremoniy

+0

可以包含更多的代码。你怎么知道它不起作用? – SomeJavaGuy

回答

0

您的代码将抛出一个编译时错误。您正尝试将一个字符'd'分配给int

得到的数字格式异常,尝试

int dmcdw; 
try{ 
dmcdw = Integer.parseInt("d"); 
}catch(NumberFormatException ex){ 
// do something with the error 
} 
+0

仍然抛出这个错误:“线程中的异常”AWT-EventQueue-0“java.lang.NumberFormatException:对于输入字符串:”d“” –

+0

嗯,它仍然无法正常工作。不知道怎么可能只是抛出一个错误消息,而不是试图将字符串转换为int? –

+0

你应该阅读关于try catch块的内容。在上面的代码中,只有当我们试图将字符串转换为int时,Numberformatexception才会被抛出。您可以在catch块中编写自己的错误消息。由于这是一个错误,请使用System.err.println(); –