3 Replies - 948 Views - Last Post: 01 March 2013 - 04:11 PM Rate Topic: -----

#1 Spenny13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-March 13

Try catch inside switch java

Posted 01 March 2013 - 03:58 PM




  value2 = Double.parseDouble(txtDisplay.getText());
           
            switch(operand){
                case  '+':
                     answer = model.add(value1, value2);
                    break;
                    
                case '-':
                     answer = model.subtract(value1, value2);    
                     break;
                        
                case '*':
                     answer = model.multiply(value1, value2);
                    break;
                    
                case '/':
                     answer = model.divide(value1, value2);
                    break;
                    
                case 'p':
                      answer = model.power(value1, value2);
                      break;
                      
                
                    
            }







hello, here is my simple switch statement, this is the code after hitting the equals button of my scientific calculator.
But i need to throw some exceptions, eq. error: cannot divide by zero, using try catch, this calculator is using the mvc pattern, can you insert switch in try catch?
I have tried but the tetx display field keeps reading infinity instead of throwing the exception which is of course incorrect so its not picking up the exception.

Is This A Good Question/Topic? 0
  • +

Replies To: Try catch inside switch java

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Try catch inside switch java

Posted 01 March 2013 - 04:00 PM

Yes you can insert switch into a try catch if you want to.

What code have you got that tries it?
Was This Post Helpful? 0
  • +
  • -

#3 Spenny13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-March 13

Re: Try catch inside switch java

Posted 01 March 2013 - 04:07 PM

Hi Ryano,


  case '/':
                    if(value2 == 0){
                    try{
                            answer= model.divide(value1, value2);
                            
                    }catch(ArithmeticException e){
                            msgExcept.showMessageDialog(this, "Divide by zero equals infinity", "Exception caught", javax.swing.JOptionPane.ERROR_MESSAGE);
                    }
                        
                    }
                     
                    break;




This post has been edited by pbl: 03 March 2013 - 03:22 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Try catch inside switch java

Posted 01 March 2013 - 04:11 PM

If value2 == 0 then you don't even need to try and divide, you know thats its undefined so you can just show the error message. No need for the try/catch.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1