Java Question Error-handling

moster67

Expert
Licensed User
Longtime User
I don't know much about Java except that it is rather similar to C#. I have managed to compile some libraries for personal use within a project of mine and they work OK.

I have now compiled another library (using a code-snippet I found) necessary for my application since the network-library does not support UDP and as long as I pass on the correct parameters from B4A to the library, it works perfectly.

However, the problem is in case there is no internet-connection available or the format of the parameters is wrong, my Android-application will close brutally. I could of course check the parameters in my application before passing them on to the library and perhaps also if there is an internet-connection available (don't know how to do that though) but I would prefer changing the code in the library to handle errors differently which will not cause my application to close.

Here below is the java-code of the library and I have remarked the lines which I believe is forcing my application to close. Anyone who could help me to correct those lines so my application will not close? Even if the library throws an error, that is OK for me as long as I can handle the error in my B4A-code (using try-catch).

Many thanks in advance for your help.

B4X:
import java.io.*;
import java.net.*;

public class WakeOnLan {
    
    public static final int PORT = 9;    
    
    public static void WakeUpPC(String[] args) {
        
              
        String ipStr = args[0];
        String macStr = args[1];
        
        try {
            byte[] macBytes = getMacBytes(macStr);
            byte[] bytes = new byte[6 + 16 * macBytes.length];
            for (int i = 0; i < 6; i++) {
                bytes[i] = (byte) 0xff;
            }
            for (int i = 6; i < bytes.length; i += macBytes.length) {
                System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
            }
            
            InetAddress address = InetAddress.getByName(ipStr);
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
            DatagramSocket socket = new DatagramSocket();
            socket.send(packet);
            socket.close();
            
                    }
        catch (Exception e) {
            //System.out.println("Failed to send Wake-on-LAN packet: + e");
            System.exit(1); 
           //NOTE exception or error here will close Android-application 
        }
        
    }
    
    private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
        byte[] bytes = new byte[6];
        String[] hex = macStr.split("(\\:|\\-)");
        if (hex.length != 6) {
            throw new IllegalArgumentException("Invalid MAC address."); 
      //NOTE also this error will close Android-application
        }
        try {
            for (int i = 0; i < 6; i++) {
                bytes[i] = (byte) Integer.parseInt(hex[i], 16);
            }
        }
        catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid hex digit in MAC address."); 
       //NOTE also this error will close Android-application
        }
        return bytes;
    }
    
   
}
 

moster67

Expert
Licensed User
Longtime User
Thanks. Resolved.

I didn't pay attention enough and did not notice the line

System.exit(1);

Usually you do not need to do too much with exceptions. Just let them go up.
The user will be able to catch those exceptions and uncaught exceptions will show the "do you want to continue?" msgbox.

Why are you calling System.exit in your code? It closes the application.
 

moster67

Expert
Licensed User
Longtime User
It's true but not very elegant if it is a commecial application. Would be better to handle those errors in B4A properly.

In any case, for the time being I have resolved my issue.


Usually you do not need to do too much with exceptions. Just let them go up.
The user will be able to catch those exceptions and uncaught exceptions will show the "do you want to continue?" msgbox.
 
Top