Can I use switch instead of a break in a statement?

Is it possible to use throw to output a switch statement without using the break keyword? Why use a throw instead of a break?

switch(number) { case 1: throw new RuntimeException("Exception number 1"); case 2: throw new RuntimeException("Exception number 2"); } 
+4
source share
7 answers

There are two cases where you can use throw to interrupt the switch flow:

  • Flow control ; in general, this is bad practice - you do not want exceptional behavior to decide where your program decides to go further.

  • An unlikely but plausible default case ; in case you click on a condition under which reaching the default value should be impossible, but will happen anyway. Somehow. A miracle. Or, if you have strict coding standards that specify that switch statements have a default case.

    Example:

     public class Test { public static enum Example { FIRST_CASE, SECOND_CASE; } public void printSwitch(Example theExampleCase) { switch(theExampleCase) { case FIRST_CASE: System.out.println("First"); break; case SECOND_CASE: System.out.println("Second"); break; default: // should be unreachable! throw new IllegalStateException( "Server responded with 724 - This line should be unreachable"); } } 
+9
source

You can use throw in the switch block, but this is not a good coding / design practice. throw intended to control an exception / error situation in the code, and not to control the execution of instructions.

Although you can use throw in switch , I would advise against using it in switch . :)

+1
source

I see some reasons to include in the switch statement.

1st: not instead of a break, but as the default case body. Consider the following example when the switch is listed:

 pubic enum E { A, B } 

The switch from the time it was first written looks like this:

 E e = ...; switch (e) { case A: ... break; case B: ... break; default: throw new IllegalStateException("Unknown enumeration value " + e); } 

A cast is a reserve for future extensions for listing E

2nd: Sometimes I have small getter functions, for example, in the Swing table model. There I use return instead of break;

 public String getColumnName(int col) { switch (col) { case 0: return "Column 0"; case 1: return "Column 1"; ... 

This is for brevity or compactness. It can be said that these returns break the flow of control. It's true. However, I think that for reasons of compactness this may be allowed here.

If you accept return instead of break , then you can also accept throw here.

 Object[] x = ...; // get some array which is not null from a function which // only returns arrays, eg OSGI services switch (x.length) { case 0: throw new IllegalArgumentException("No service defined"); case 1: return x[0]; // return the only available service default: ... // evaluate all services and return the best matching one return x[...]; } 
+1
source

You are talking only about one scenario in which you need to throw an exception. the switch is designed to start the housing and its fracture. If it is a method, and if you use only throw, then how will you return a value from this method? In simple words, throw will return a method, but there will be no break. You can continue to run your code after switching with a break, but not with a throw.

0
source

break is used to prevent a fall until the next time you throw an exception, there will be no fall down

  case 1: throw new UnsupportedOperationException(); break; <-- compile error, unreachable code 

As a rule, it makes sense to throw an exception in another sentence

  switch (op) { case 1: // do something break; ... default: throw new UnsupportedOperationException(); } 
0
source

There are several problems using throw instead of break;

1) when you throw some kind of exception, the rest of the function stops executing.

t

 int m=0; switch (m) { case 0: y=10; break; case 2: y=11; break; default: break; } System.out.println(y);//this y will never be printed if Exceptions are thrown 

2) Exceptions usually represent some kind of error condition, and not every time you have to create an exception.

3) To get the value "Exception Number 1" from

 throw new RuntimeException("Exception number 1"); 

you need to block the catch block and call e.getMessage ();

as:

 catch(RuntimeException e){ String str=e.getMessage(); } 

this is even more overhead, and the data you can pass is just a String object.

Conclusion: - It is better to use a normal gap.

0
source

throw Cannot be used instead of a break in a switch statement. because throw is used only for exceptions, while the switch is used for conditional purposes ...

-1
source

Source: https://habr.com/ru/post/1481094/


All Articles