- Java: Mysterious Java uncaught exception handler [with code]
- package staticexception;
- import java.net.InterfaceAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Enumeration;
- import java.util.List;
- import javax.swing.UIManager;
- public class StaticException {
- // Don't need this fully implemented.
- public static class IPAddress {
- public static IPAddress getBroadcast(IPAddress mask, IPAddress myip) {
- return new IPAddress();
- }
- public IPAddress() {}
- public IPAddress(int maskval) {}
- public IPAddress(byte[] addr) {}
- public IPAddress mask(IPAddress netmask) {
- return this;
- }
- public int prefixLength() {
- return 0;
- }
- }
- public static class Network {
- public IPAddress broadcast, netmask, ip;
- boolean remember;
- public Network(IPAddress br, IPAddress nm, IPAddress ip) {
- broadcast = br;
- netmask = nm;
- this.ip = ip;
- }
- boolean match(IPAddress ip) {
- IPAddress a = ip.mask(netmask);
- IPAddress b = this.ip.mask(netmask);
- return (a.equals(b));
- }
- @Override
- public String toString() {
- return ip.toString() + "/" + netmask.prefixLength();
- }
- }
- static List<Network> my_networks;
- static void enumerateNetworks() {
- my_networks = new ArrayList<Network>();
- Enumeration<NetworkInterface> nets = null;
- try {
- nets = NetworkInterface.getNetworkInterfaces();
- } catch (SocketException ex) {
- ex.printStackTrace();
- }
- for (NetworkInterface netint : Collections.list(nets)) {
- for (InterfaceAddress address : netint.getInterfaceAddresses()) {
- // *** Exception would occur on the next line when
- // *** address.getAddress() would return null
- byte[] addr = address.getAddress().getAddress();
- if (addr.length == 4 && addr[0] != 127) {
- int prefixlen = address.getNetworkPrefixLength();
- int maskval = -1 << (32 - prefixlen);
- IPAddress mask = new IPAddress(maskval);
- //my_netmask = mask;
- System.out.println("Netmask = " + mask);
- IPAddress myip = new IPAddress(addr);
- //my_ip_address = myip;
- System.out.println("Local IP = " + myip);
- IPAddress broadcast = IPAddress.getBroadcast(mask, myip);
- System.out.println("Broadcast = " + broadcast);
- my_networks.add(new Network(broadcast, mask, myip));
- System.out.print(address.getAddress().getAddress().length + " ");
- System.out.print(address.getAddress() + " ");
- System.out.print(address.getAddress().getHostAddress() + " ");
- System.out.println(address.getNetworkPrefixLength());
- }
- }
- }
- }
- static private void setupNetwork() {
- System.setProperty("java.net.preferIPv4Stack","true");
- enumerateNetworks();
- // ... stuff that would happen after the exception
- }
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {}
- setupNetwork();
- // ... stuff that would happen after the exception
- }
- }