Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>When I try using a try, catch IOException, it gives me an error that cannot have a catch in the same body as a try</p> </blockquote> <p>The error is self-explanatory. The following code is illegal: </p> <pre><code>try{ catch(Exception ex){ } } </code></pre> <p>You want this: </p> <pre><code>try{ //stuff i need to do } //close the try catch(IOException ioex) { //log and recover } </code></pre> <p><strong>=UPDATE</strong> </p> <p>Based on the code block below (the same that OP is talking about in case it is changed later) The actual error message that is being displayed is this: </p> <blockquote> <p>Unreachable catch block for IOException. This exception is never thrown from the try statement body GUI.java</p> </blockquote> <p>The reason this occurs is that there is nothing that generates an <code>IOException</code> in your code, the only exception you can define without an explicit throw from one of your functions is the top level <code>Exception</code>.</p> <pre><code>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);} } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload