Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

if statement in a catch exception statement

  • 02-04-2008 4:35pm
    #1
    Closed Accounts Posts: 20


    im outputing a message at the end of this code to output an error message if the there is and error and to keep the label(gui) clear if its not.can this be done.

    {

    public void actionPerformed(ActionEvent e)

    {
    try{
    int money = Integer.parseInt(display.getText());
    machine.insertMoney(money);
    display.setText("");


    if (machine.getBalance()<machine.getPrice())

    {
    outputmsg2.setText("Please input :" + (machine.getPrice() - machine.getBalance())+ " more coins");
    }

    else
    {
    machine.printTicket();
    outputmsg2.setText("Change:" + machine.refundBalance());
    outputmsg3.setText("Ticket Sold Thank You");


    }

    }
    catch(Exception ex1)
    {
    outputmsg4.setText("Please insert a number!!not nothing or letter");
    else outputmsg4.setText("");
    }
    }
    }

    }
    any input would be helpful


Comments

  • Registered Users, Registered Users 2 Posts: 86,729 ✭✭✭✭Overheal


    wheres your throw statement?

    in practice though youre only really meant to use exceptions for exceptional circumstances, such as hardware or network failure. it is much more ideal for example, to handle null and char/string values inside the code.


  • Registered Users, Registered Users 2 Posts: 1,834 ✭✭✭dloob


    So you want to output the error message if an exception is thrown, otherwise the message should be blank, right?

    Well if we get to the end of the try block then an exception is not going to be thrown.
    So if we put the outputmsg4.setText(""); as the last line of the try block then it will be cleared only if nothing has caused an exception.


  • Registered Users, Registered Users 2 Posts: 151 ✭✭sailorfoley


    Hey,

    catch the NumberFormatException and display your message there. NumberFormatException will be thrown if display.getText() does not return a parse able string.

    Hope this helps.


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    You should only have a try around the Integer.parseInt, as you have a very open exception there. The catch will only be called if there IS an error, so having anything else in there won't mean anything (especially an "else"? Dunno if that would even work!). If you want to set it after the transaction is successful, set the outputmsg4 text after "outputmsg3.setText("Ticket Sold Thank You");"


    EDIT: Holy **** was I beaten.


  • Closed Accounts Posts: 20 heavy482


    Problem solved thanks:)


  • Advertisement
  • Closed Accounts Posts: 8 Zan


    theres no need for the try catch at all.

    int money = default(int);
    if (!int.Tryparse(display.getText(), out money))
    outputmsg4.setText("Please insert a number!!not nothing or letter");
    else
    ... do the rest of your code here


  • Registered Users, Registered Users 2 Posts: 151 ✭✭sailorfoley


    From the syntax above i think this is java.

    i dont think there is a TryParse in java but in .Net TryParse would be the best way.


Advertisement