【问题标题】:Catch int Exception捕捉 int 异常
【发布时间】:2016-01-13 09:44:33
【问题描述】:

我给 int dmcdw 值 'd' 并希望通过异常捕获错误,但它不起作用。

这是我的代码的一部分:

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)

【问题讨论】:

  • 你说的“它不工作”是什么意思
  • 您能否包含更多代码。你怎么知道它不起作用?
  • @AdamKortylewicz 我不想将 String 'd' 转换为 int,我想获得一个有效的异常处理程序,因此不会发生此类错误
  • 就像错误消息所说,d 不是变量。你是说'd'(角色)吗?

标签: java exception


【解决方案1】:

您的代码将引发编译时错误。您正在尝试将字符 'd' 分配给 int

获取数字格式异常,试试

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

【讨论】:

  • 仍然抛出此错误:“线程中的异常“AWT-EventQueue-0”java.lang.NumberFormatException:对于输入字符串:“d””
  • 嗯,还是不行。以某种方式可能只是抛出一条错误消息而不是尝试将 String 转换为 int 吗?
  • 你应该阅读 try catch 块。在上面的代码中,只有当我们尝试将字符串转换为 int 时,才会抛出 Numberformatexception。您可以在 catch 块中编写自己的错误消息。既然是错误,就使用 System.err.println();
猜你喜欢
  • 2012-01-10
  • 2020-04-20
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多