Reputation: 433
I'm just wondering if it is possible to create something like this:
String variable = "name of the variable I'm checking";
for (int i = 0; i < 16; i++)
{ // if it's possible what type should the check variable be?
check = (array[0].equals("value1") || array[0].equals("value2") ?
"some value or null?" : throwException(i, variable);
}
public void throwException(int index, String name) throws TelegrammException
{
throw new TelegrammException("Wrong telegramm format at position: "
+ index + "; Name: " + name);
}
If this is not possible could you suggest a good practice of how to do something similar?
Upvotes: 1
Views: 3000
Reputation: 121998
No, it's not possible with ternary operator. At least not in Java. Both expressions should resolved to return the same thing. Otherwise the compiler emits an error. Your second expression is not returning anything apart from the throwing expression. It won't work that way.
From the JLS, regarding the ternary operator:
The first expression must be of type
boolean
orBoolean
, or a compile-time error occurs.It is a compile-time error for either the second or the third operand expression to be an invocation of a
void
method.
You should consider doing it with traditional approach.
for (int i = 0; i < 16; i++)
{ // if it's possible what type should the check variable be?
if (array[0].equals("value1") || array[0].equals("value2"))
{
check = "some value or null?";
} else {
throwException(i, variable);
}
}
Upvotes: 1
Reputation: 140319
Sure: make your method "return" String
:
public String throwException(int index, String name) throws TelegrammException
{
throw new TelegrammException("Wrong telegramm format at position: "
+ index + "; Name: " + name);
}
Now, this never actually returns a String
because it doesn't complete normally. But you can now use this in a conditional expression:
String result = condition ? "Something" : throwException(index, name);
But: this is an abuse of syntax. Just stick with a plain old if statement:
if (condition) {
result = "Something";
} else {
throwException(index, name);
}
You might also then want to consider making the return type of the method TelegrammException
:
public TelegrammException throwException(int index, String name) throws ...
This allows you to throw
at the call site:
throw throwException(index, name);
which allows you to indicate to the compiler (and humans reading your code) that execution doesn't continue beyond there, which may help you with things like definite assignment/return value requirements.
Upvotes: 1
Reputation: 44130
Technically yes if you change the return type of your throwException
method to String
.
public String throwException(int index, String name) throws TelegrammException
However, having a method which ostensibly returns a string which in fact always throws is very unconventional and would almost certainly confuse future readers of your code. The idiomatic way to express your intent is to not use a ternary expression at all:
if (array[0].equals("value1") || array[0].equals("value2"))
{
check = "some value or null?";
}
else
{
throwException(i, variable);
}
Upvotes: 2