Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 488 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'if' must have both main and 'else' branches if used as an expression

#1
I have the code below working before, now compiler halts and marks both `if` statements and says:

**'if' must have both main and 'else' branches if used as an expression**

But as you see this is not an expression, but just a simple equality statement and a conditional statement next to it.

```
try {
val json_string = responseBody!!.string()
val jsonObject = JSONObject(json_string)
if (jsonObject.has("version")) {

val remoteVersion = jsonObject.getInt("version")
if (remoteVersion > BuildConfig.VERSION_CODE) {
handler.post {
showInstallNewVersionDialog()
}
}
}
} catch (e: Exception) {
e.message?.let { Log.e(Constants.TAG, e.message!!) }

}
```

The funny part is if I added empty `else` tags, it will run but will warn to remove empty `else` statements:

```
if (jsonObject.has("version")) {

val remoteVersion = jsonObject.getInt("version")
if (remoteVersion > BuildConfig.VERSION_CODE) {
handler.post {
showInstallNewVersionDialog()
}
} else {}
} else {}
```
Reply

#2
If IDE tells you that `'if' must have both main and 'else' branches if used as an expression` then it is so. Most likely this try-catch construction is defined as a custom getter of a variable or as [single-expression function][1].

An example:

```
val aVariable =
try {
if (System.currentTimeMillis() == 0L) {
println("It is 1970-01-01")
}
} catch (e: Exception) {
// empty
}

fun aFunction() =
try {
if (System.currentTimeMillis() == 0L) {
println("It is 1970-01-01")
}
} catch (e: Exception) {
// empty
}
```

And the IDE (lint) shows me an error even before compiling. Same error for the function.

[![Error message][2]][2]

To fix this issue you either introduce else statement OR redeclare this variable as a function (or update the function you have). This function will work fine as it always returns `Unit` even if you do not have any code in it.

```
fun aFunction() {
try {
if (System.currentTimeMillis() == 0L) {
println("It is 1970-01-01")
}
} catch (e: Exception) {
// empty
}
}
```

When you use single-expression functions or getters you **MUST** return a value. That is why `else` part is required.

[1]:

[To see links please register here]

[2]:
Reply

#3
The issue is that Kotlin sees `try {} catch {}` construct as a "function" that returns a value, which is not the same as Java `try {} catch {}`.
As a result compiler assumes that you "forgot" to code the `else` branches even if it is clearly the case of you not wanting to return any value.
Kotlin sometimes is ludicrous.

i.e. you can write:

val t = try {
"test"
} catch (e: Exception) {
"error"
}
println(t)
Reply

#4
As noted above, "Kotlin sometimes is ludicrous". In my case I was trying to do something in the catch statement:

fun x() {
try {
doSomething()
doSomethingIfNoException()
} catch (e:Exception) {
if© doSomethingForException()
else ""
}
}

Kotlin insisted on the else in the catch. Putting the else"" was the easiest fix I could find.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through