java - new inetaddress without checked exception

f.ald :

I want instances of InetAddress but don't want to put it in a try-catch like every factory method of that class forced to :

InetAddress addr = InetAddress.getByAddress(ipAddr);

Is there anyway to instantiate InetAddress from String ip that does not throw any checked exception?

user11044402 :

You can't. InetAddress.getByAddress(...) throws a UnknownHostException which is a checked exception because it is a subclass of Exception. You must handle every checked excpetion by either catching it or throwing it yourself. There is no way around this. Somewhere in you code you will have to catch it or your program will crash.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165823&siteId=1