Tuesday, September 15, 2015

Orphaned case error in Java Switch case

Orphaned Case Error in Java is one of the rarest errors you can see in Java. You can see this error  in cases like


  • When you write your case statements outside your switch statement. 

 switch (var) {  
       case 1:  
       //some code here....  
       break;   
 }  
   case 2:  
       //some code here....  
       break;


  • If by mistake if you terminated your switch testaments unexpectedly 


 switch (var); { <--- statement terminated by ;   
     case 1:   
     //some code here....   
     break;    
  }  

And in another case  where a case statement doesn't belong to switch and became orphan.


Note : Errors like this won't be seen if you are using an IDE since they compile your code on the fly and show the error message immediately, only traditional compilers will show this error, when you compile through your command line.






7 comments:

  1. How to correct the orphaned case?

    ReplyDelete
    Replies
    1. switch (var) {
      case 1:
      //some code here....
      break;
      }
      case 2:
      //some code here....
      break;


      If by mistake if you terminated your switch testaments unexpectedly


      switch (var); { <--- statement terminated by ;
      case 1:
      //some code here....
      break;
      }

      And in another case where a case statement doesn't belong to switch and became orphan.


      Note : Errors like this won't be seen if you are using an IDE since they compile your code on the fly and show the error message immediately, only traditional compilers will show this error, when you compile through your command line.

      Delete
  2. U need to remove the semicolons or check whether you have closed the brackets earlier

    ReplyDelete
  3. how to fix orphand case error the code where i found mistake case1:res=no1+no2;

    ReplyDelete
  4. switch (var) {
    case 1:
    //some code here....
    break;
    }
    case 2:
    //some code here....
    break;


    If by mistake if you terminated your switch testaments unexpectedly


    switch (var); { <--- statement terminated by ;
    case 1:
    //some code here....
    break;
    }

    And in another case where a case statement doesn't belong to switch and became orphan.


    Note : Errors like this won't be seen if you are using an IDE since they compile your code on the fly and show the error message immediately, only traditional compilers will show this error, when you compile through your command line.

    ReplyDelete