Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Try catch inside switch java

$
0
0



  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.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>