Orphaned Case Java
In the previous section, we have discussed the switch statement in detail. In this section, we are going to discuss the rarest error i.e. orphaned case error in Java.
Orphaned Case Error
In Java, it is the rarest error that does not occur usually. The error occurs while dealing with the switch statement.
Reasons to Raise Orphaned Case Error
1. The keyword switch may be capitalizing. For example:
2. The cases may be out of the switch statement. For example, in the following code snippet case 2 is out of the switch statement.
3. If switch statement unexpectedly terminated.
4. If we use comparison expression in the case statement.
How to fix an orphaned case error?
Example of Orphaned Case Error
When we compile the above Java program, it gives an orphaned case error.
In order to overcome the above error, write the switch statement in small letters. After making the changes, compile and run the above code. It gives the following output:
You are eligible for vote.
Let's see another Java program that raises orphaned case errors.
When we compile the above program, it gives an orphaned case error because the cases having uppercase letters are outside the switch statement.
We can resolve the above error by wrapping the case statements inside the switch statement.