Search code examples
javamethodsunreachable-statement

What is the meaning of Unreacheable Statement


It says "int addition = t+r;" is an unreachable statement. what does that mean? How to correct it?

public class parseMETHOD {
       public static void main(String[] args) {
           int a=9;
           int b=45;
           int result=calFunction(a,b);
           System.out.println(result);
       }

       private static int calFunction(int t,int r) {
           throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools |
   Templates.
           int addition = t+r;
           return addition;

       } }

Solution

  • The statement is logically unreachable due to some your program's control flow. You are throwing an exception that isn't caught within your function.

    Please delete the throw statement and the word "Template"

       public class parseMETHOD {
           public static void main(String[] args) {
               int a=9;
               int b=45;
               int result=calFunction(a,b);
               System.out.println(result);
           }
    
           private static int calFunction(int t,int r) {
    
               int addition = t+r;
               return addition;
    
           }
       }
    

    Also, good naming conventions state that you should capitalize class names.