Jia Rong
Jia Rong

Reputation: 3

Try catch block issues

My code doesn't terminate at catch block and never return "ERROR" even though I feed cases where the input will never match "song", "joke" or "story". And always returns me the chatbot's name. Is there a reason why???

String execute(String command) {
    String result = "";
    try{                                                                                        
        switch(command) {                                                                      
            case "song":
                result += " sings: Like a Rolling Stone ...";                                                                                                   
                break;
            case "joke":                                                                             
                result += " says: Why are Communists bad Java programmers?They dont't like classes.";                                                                                   
                break;                                                                      
            case "story":                                                                              
                result += " says: A long long time ago ...";                                        
                break;                                                              
        } 
    } catch (InputMismatchException e) {
            return "ERROR";
    } 
    return this.ChatBotName + result;
} 

Upvotes: 0

Views: 35

Answers (2)

ldwformat
ldwformat

Reputation: 21

The reason is that the code is running currect and never throw any error that can be caught. No matching branch in switch statement is not an error. You can check more details about the switch statement from here

Upvotes: 1

Akash Jain
Akash Jain

Reputation: 336

Instead of using try and catch block can you not use default case from switch case.

String execute(String command) {
    String result = "";                                                                        
    switch(command) {                                                                      
        case "song":
            result += " sings: Like a Rolling Stone ...";                                                                                                   
            break;
        case "joke":                                                                             
            result += " says: Why are Communists bad Java programmers?They dont't like classes.";                                                                                   
            break;                                                                      
        case "story":                                                                              
            result += " says: A long long time ago ...";                                        
            break;  
        default:
            return "ERROR";
            break;                                                
    } 
    return this.ChatBotName + result;
} 

Upvotes: 0

Related Questions