getHostName returns null INSTEAD of throwing exceptions

master
Robinson 2023-02-22 18:21:57 +01:00
parent f616ec60b0
commit 20ef556c41
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
2 changed files with 10 additions and 9 deletions

View File

@ -111,9 +111,7 @@ object Address {
*
* @param address The address to look up
*
* @return The associated host name
*
* @throws UnknownHostException There is no hostname for the address
* @return The associated host name or null if there is no hostname for the address
*/
@Throws(UnknownHostException::class)
fun getHostName(address: InetAddress): String? {
@ -124,7 +122,7 @@ object Address {
val records = try {
client.query(name.toString(true), DnsRecordType.PTR)
} catch (ignored: Throwable) {
throw UnknownHostException("unknown address")
return null
} finally {
client.stop()
}

View File

@ -369,8 +369,9 @@ class AddressTest : TestCase() {
@Throws(UnknownHostException::class)
fun test_getAllByName_invalid() {
try {
getAllByName("example.invalid")
fail("UnknownHostException not thrown")
if (getAllByName("example.invalid").isNotEmpty()) {
fail("getAllByName should be empty!")
}
} catch (ignored: UnknownHostException) {
}
@ -379,7 +380,8 @@ class AddressTest : TestCase() {
assertEquals("127.0.0.1", byName[0]!!.hostAddress)
assertEquals("0:0:0:0:0:0:0:1", byName[1]!!.hostAddress)
} catch (ignored: UnknownHostException) {
fail("UnknownHostException thrown")
ignored.printStackTrace()
fail("${ignored.javaClass} thrown!")
}
}
@ -402,8 +404,9 @@ class AddressTest : TestCase() {
assertEquals("a.root-servers.net.", out)
try {
getHostName(InetAddress.getByName("192.168.1.1"))
fail("UnknownHostException not thrown")
if (getHostName(InetAddress.getByName("192.168.1.1")) != null) {
fail("getHostName should be null!")
}
} catch (ignored: UnknownHostException) {
}
}