Hi. Our homework is to use try...catch block in our calculator applet. I don't understand why the "catch" does not work. It will always go straight to the "finally". Here's the code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Label;
import javax.swing.*;


public class clsCalculator extends Applet implements ActionListener
{
    Label lblTitle= new Label("Standard Calculator");
    Label lblFirst=new Label("Enter 1st Number: ");
    Label lblSecond=new Label("Enter 2nd Number: ");
    Label oper=new Label("Click the Operation: ");
    Label ans=new Label("The Answer is: ");
    TextField txtFirst = new TextField(5);
    TextField txtSecond = new TextField(5);
    TextField txtAns = new TextField(5);
    Button btnPlus=new Button("+");
    Button btnSub=new Button("-");
    Button btnMul=new Button("*");
    Button btnDiv=new Button("/");
    Button btnEqual=new Button("=");
    Button btnClear=new Button("Clear");
    double dFirst,dSecond,dAns;



    public void init()
    {
        add(lblTitle);
        add(lblFirst);
        add(txtFirst);
        add(lblSecond);
        add(txtSecond);
        add(oper);
        add(btnPlus);
        add(btnSub);
        add(btnMul);
        add(btnDiv);
        add(btnEqual);
        add(btnClear);
        add(ans);
        add(txtAns);

        btnPlus.addActionListener(this);
        btnSub.addActionListener(this);
        btnMul.addActionListener(this);
        btnDiv.addActionListener(this);
        btnEqual.addActionListener(this);
        btnClear.addActionListener(this);
        txtAns.setEditable(false);



    }//init

    public void actionPerformed(ActionEvent ae)
    {
    String sNum1, sNum2, sAns;
    sNum1=txtFirst.getText();
    sNum2=txtSecond.getText();
    dFirst=Double.parseDouble(sNum1);
    dSecond=Double.parseDouble(sNum2);

    try
    {
    if(ae.getSource()==btnPlus)
    {   dAns=dFirst+dSecond;
        btnSub.setEnabled(false);
        btnMul.setEnabled(false);
        btnDiv.setEnabled(false);
    }
    if(ae.getSource()==btnSub)
    {   dAns=dFirst-dSecond;
        btnPlus.setEnabled(false);
        btnMul.setEnabled(false);
        btnDiv.setEnabled(false);
    }
    if(ae.getSource()==btnMul)
    {   dAns=dFirst*dSecond;
        btnSub.setEnabled(false);
        btnPlus.setEnabled(false);
        btnDiv.setEnabled(false);
    }
    if(ae.getSource()==btnDiv)
    {   
            dAns=dFirst/dSecond;
            btnSub.setEnabled(false);
            btnMul.setEnabled(false);
            btnPlus.setEnabled(false);

    }
    if(ae.getSource()==btnEqual)
    {   sAns=Double.toString(dAns);
        txtAns.setText(sAns);
        btnEqual.setEnabled(false);
    }
    if(ae.getSource()==btnClear)
    {   txtFirst.setText("");
        txtSecond.setText("");
        txtAns.setText("");
        btnSub.setEnabled(true);
        btnPlus.setEnabled(true);
        btnDiv.setEnabled(true);
        btnMul.setEnabled(true);
        btnEqual.setEnabled(true);

    }
    }//try
    catch(ArithmeticException e)
    {txtAns.setText("INVALID!");
            JOptionPane.showMessageDialog(null,"Can't Divide by Zero!", "Error",JOptionPane.WARNING_MESSAGE);
    }
    catch(NumberFormatException e)
    {   txtAns.setText("INVALID!");
        JOptionPane.showMessageDialog(null,"Invalid Input!", "Error",JOptionPane.WARNING_MESSAGE);
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,e.getMessage(), "Error",JOptionPane.WARNING_MESSAGE);
    }

    finally
    {
        JOptionPane.showMessageDialog(null,"blablabla", "Error",JOptionPane.INFORMATION_MESSAGE);
    }
    }//action


}//cls

can someone please tell me what the problem is? thanks! :D

Edited 3 Years Ago by pritaeas: Moved to Java.

Run it in the debugger, or add some debug output statements, so you know where it is generating the exception.

The exception is probably a subclass of Error. To catch all possible exceptions, use "Throwable" instead of "Exception" in the catch clause. And use the debugger or the display to determine what exception is happening. Print the stack trace to determine where the error is happening.

better question: IS there an Exception thrown that should be caught?

 finally
    {
        JOptionPane.showMessageDialog(null,"blablabla", "Error",JOptionPane.INFORMATION_MESSAGE);
    }

the finally block will run, whether there was an exception or not, so just by default printing error there will make it seem like there was an exception, but that doesn't automatically make it so.

This question has already been answered. Start a new discussion instead.