如何询问“是否存在异常,然后执行此操作”java

时间:2012-11-11 04:36:12

标签: java exception exception-handling

我想问一下,如果发生异常,则在文本字段中显示某个字符串。当我尝试使用try时,捕获IOException,它给了我一个错误,它不能在try和同一个body中有一个catch。这可能应该在动作执行的方法中完成。

GUI:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GUI extends JFrame implements ActionListener
{
JPanel buttonPanel, topPanel, operationPanel;
JTextField display = new JTextField(20);

doMath math = new doMath();
String s = "";
String b= "";
//int counter;

JButton Num1;
JButton Num2;
JButton Num3;
JButton Num4;
JButton Num5;
JButton Num6;
JButton Num7;
JButton Num8;
JButton Num9;
JButton Num0;

JButton Add;
JButton Sub;
JButton Mult;
JButton Div;
JButton Eq;
JButton Clr;
JButton Space;
public GUI()
{
    super("Calculator");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout (2,1));

    buttonPanel = new JPanel();        
    buttonPanel.setLayout(new GridLayout(5, 4));
    buttonPanel.add(Num1 = new JButton("1"));
    buttonPanel.add(Num2 = new JButton("2"));
    buttonPanel.add(Num3 = new JButton("3"));
    buttonPanel.add(Num4 = new JButton("4"));
    buttonPanel.add(Num5 = new JButton("5"));
    buttonPanel.add(Num6 = new JButton("6"));
    buttonPanel.add(Num7 = new JButton("7"));
    buttonPanel.add(Num8 = new JButton("8"));
    buttonPanel.add(Num9 = new JButton("9"));
    buttonPanel.add(Num0 = new JButton("0"));        
    buttonPanel.add(Clr = new JButton("C"));
    buttonPanel.add(Eq = new JButton("="));
    buttonPanel.add(Add = new JButton("+"));
    buttonPanel.add(Sub = new JButton("-"));
    buttonPanel.add(Mult = new JButton("*"));
    buttonPanel.add(Div = new JButton("/"));
    buttonPanel.add(Space = new JButton("Space")); 

    Num1.addActionListener(this);
    Num2.addActionListener(this);
    Num3.addActionListener(this);
    Num4.addActionListener(this);
    Num5.addActionListener(this);
    Num6.addActionListener(this);
    Num7.addActionListener(this);
    Num8.addActionListener(this);
    Num9.addActionListener(this);
    Num0.addActionListener(this);
    Clr.addActionListener(this);
    Eq.addActionListener(this);
    Add.addActionListener(this);
    Sub.addActionListener(this);
    Mult.addActionListener(this);
    Div.addActionListener(this);
    Space.addActionListener(this);

    topPanel = new JPanel();         
    topPanel.setLayout(new FlowLayout());
    topPanel.add(display);

    add(mainPanel);

    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);

    setVisible(true);

}

public void actionPerformed(ActionEvent e) 
{
    JButton source = (JButton)e.getSource();
    String text = source.getText();

try{
    if (text.equals("=")) 
    {
        doMath math = new doMath();

        b = b.replaceAll("\\s+", " ");

        int result = math.doMath1(b);

        String answer = ""+result;
        display.setText(answer);

    }

    else if(text.equals("Space"))
    {        

            b+=" ";
            display.setText(b);

    }

    else if (text.equals("C"))
    {
        b = "";

        display.setText(b);
    }
    else if (text.equals("+") || text.equals("-") || text.equals("*") ||     text.equals("/"))
    {
         b += (" "+(text)+ " ");

        display.setText(b);
    }
    else 
    {

        b += (text);

        display.setText(b);

    }
    }
catch(IOException o)
{display.setText(b);} 

}
}

1 个答案:

答案 0 :(得分:5)

  

当我尝试使用try时,捕获IOException,它会给我一个错误   不能尝试在同一个体内捕捉

错误不言自明。以下代码是非法的:

try{  
    catch(Exception ex){  
         }  
   }

你想要这个:

try{  
    //stuff i need to do  
   } //close the try  
  catch(IOException ioex)  
  {  
     //log and recover  
  }

=更新

基于下面的代码块(与OP稍后讨论的相同)以后显示的实际错误消息是:

  

IOException的无法访问的catch块。从来没有这个例外   从try语句体GUI.java抛出

发生这种情况的原因是,在您的代码中没有任何内容可以生成IOException,您可以定义的唯一例外是没有从您的某个函数中显式抛出的唯一例外是顶级Exception。< / p>

public class GUI extends JFrame implements ActionListener
{
JPanel buttonPanel, topPanel, operationPanel;
JTextField display = new JTextField(20);

doMath math = new doMath();
String s = "";
String b= "";
//int counter;

JButton Num1;
JButton Num2;
JButton Num3;
JButton Num4;
JButton Num5;
JButton Num6;
JButton Num7;
JButton Num8;
JButton Num9;
JButton Num0;

JButton Add;
JButton Sub;
JButton Mult;
JButton Div;
JButton Eq;
JButton Clr;
JButton Space;
public GUI()
{
    super("Calculator");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout (2,1));

    buttonPanel = new JPanel();        
    buttonPanel.setLayout(new GridLayout(5, 4));
    buttonPanel.add(Num1 = new JButton("1"));
    buttonPanel.add(Num2 = new JButton("2"));
    buttonPanel.add(Num3 = new JButton("3"));
    buttonPanel.add(Num4 = new JButton("4"));
    buttonPanel.add(Num5 = new JButton("5"));
    buttonPanel.add(Num6 = new JButton("6"));
    buttonPanel.add(Num7 = new JButton("7"));
    buttonPanel.add(Num8 = new JButton("8"));
    buttonPanel.add(Num9 = new JButton("9"));
    buttonPanel.add(Num0 = new JButton("0"));        
    buttonPanel.add(Clr = new JButton("C"));
    buttonPanel.add(Eq = new JButton("="));
    buttonPanel.add(Add = new JButton("+"));
    buttonPanel.add(Sub = new JButton("-"));
    buttonPanel.add(Mult = new JButton("*"));
    buttonPanel.add(Div = new JButton("/"));
    buttonPanel.add(Space = new JButton("Space")); 

    Num1.addActionListener(this);
    Num2.addActionListener(this);
    Num3.addActionListener(this);
    Num4.addActionListener(this);
    Num5.addActionListener(this);
    Num6.addActionListener(this);
    Num7.addActionListener(this);
    Num8.addActionListener(this);
    Num9.addActionListener(this);
    Num0.addActionListener(this);
    Clr.addActionListener(this);
    Eq.addActionListener(this);
    Add.addActionListener(this);
    Sub.addActionListener(this);
    Mult.addActionListener(this);
    Div.addActionListener(this);
    Space.addActionListener(this);

    topPanel = new JPanel();         
    topPanel.setLayout(new FlowLayout());
    topPanel.add(display);

    add(mainPanel);

    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);

    setVisible(true);

}

public void actionPerformed(ActionEvent e) 
{
    JButton source = (JButton)e.getSource();
    String text = source.getText();

try{
    if (text.equals("=")) 
    {
        doMath math = new doMath();

        b = b.replaceAll("\\s+", " ");

        int result = math.doMath1(b);

        String answer = ""+result;
        display.setText(answer);

    }

    else if(text.equals("Space"))
    {        

            b+=" ";
            display.setText(b);

    }

    else if (text.equals("C"))
    {
        b = "";

        display.setText(b);
    }
    else if (text.equals("+") || text.equals("-") || text.equals("*") ||     text.equals("/"))
    {
         b += (" "+(text)+ " ");

        display.setText(b);
    }
    else 
    {

        b += (text);

        display.setText(b);

    }
    }
catch(IOException o)
{display.setText(b);} 

}
}