Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - new inetaddress without checked exception

Tags:

java

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?

like image 697
f.ald Avatar asked Dec 07 '25 06:12

f.ald


1 Answers

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!