Unreported exception java.lang.Exception; must be caught or declared to be thrown

Unreported exception java.lang.Exception; must be caught or declared to be thrown

public static byte[] m16h(byte[] m) throws Exception

The signature of your method indicates that an Exception is susceptible of being thrown.

This means that the exception either :

  1. Must be handled by the caller
    try {
        System.out.println(xor(m16h(add(xor(xor(m16h(add(k1, m16h(add(k2, m16h(k3))))), k3), k2), k1)), k3));
    } catch (Exception e) {
        e.printStackTrace();
    }
    
  2. Must be rethrowed by the caller
    public static void main(String[] args) throws Exception
    

In your main method, the call to m16h may lead to an exception being thrown. In this case, you have two choices:

  • handle the exception yourself in the main method.
// in the main
try {
    System.out.println(xor(m16h(...));
} catch(Exception e) {
    // do something, e.g. print e.getMessage()
}
  • indicate that the main method can throw an exception, by appending throws Exception to its declaration.

public static void main(String args[]) throws Exception

Unreported exception java.lang.Exception; must be caught or declared to be thrown

Surround the line where you call this method with a try/catch block as follows:

try {
  System.out.println(xor(m16h(add(xor(xor(m16h(add(k1, m16h(add(k2, m16h(k3))))), k3), k2), k1)), k3));
}catch (Exception e){
  System.out.println (e.getMessage());
}

Leave a Reply

Your email address will not be published.