未报告的异常 java.lang.Exception; 必须被捕获或声明被抛出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37424284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:19:08  来源:igfitidea点击:

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

java

提问by pee2pee

I tried compiling the below but get the following around m16h(x):

我尝试编译以下内容,但在 m16h(x) 附近得到以下内容:

Line: 16
unreported exception java.lang.Exception; must be caught or declared to be thrown

Not sure why though. I've tried various things but it seems I am doing it right.

不知道为什么。我尝试了各种方法,但似乎我做得对。

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Test{ 

   public static void main(String args[]){

        byte[] k1 = parseHexString("eb35a6c92c3b8c98033d739969fcc1f5ee08549e", 20);
        byte[] k2 = parseHexString("57cb8b13a1f654de21104c551c13d8820b4d6de3", 20);
        byte[] k3 = parseHexString("c4c4df2f8ad3683677f9667d789f94c7cffb5f39", 20);

      System.out.println(k1);
      System.out.println(k2);
      System.out.println(k3);
      System.out.println(xor(m16h(add(xor(xor(m16h(add(k1, m16h(add(k2, m16h(k3))))), k3), k2), k1)), k3));

   }

    public static byte[] m16h(byte[] m) throws Exception {
        return parseHexString(SHA1(m), 20);
    }

   private static byte[] xor(byte[] x, byte[] y) {
        int l = x.length;
        if (l != y.length) {
            return null;
        }
        byte[] ob = new byte[l];
        for (int i = 0; i < l; i++) {
            ob[i] = (byte) (x[i] ^ y[i]);
        }
        return ob;
    }

    public static byte[] parseHexString(String x, int len) {
        byte[] ret = new byte[len];
        for (int i = 0; i < len; i++) {
            ret[i] = (byte) Integer.parseInt(x.substring(i * 2, (i * 2) + 2), 16);
        }
        return ret;
    }



    public static byte[] add(byte[] x, byte[] y) {
        byte[] added = new byte[(x.length + y.length)];
        System.arraycopy(x, 0, added, 0, x.length);
        System.arraycopy(y, 0, added, x.length, y.length);
        return added;
    }



    public static String SHA1(byte[] c) throws NoSuchAlgorithmException {
        return base16encode(MessageDigest.getInstance("SHA-1").digest(c));
    }

    public static String base16encode(byte[] data) {
        String res = "";
        for (byte b : data) {
            res = String.format("%s%02x", new Object[]{res, Byte.valueOf(b)});
        }
        return res;
    }
}

采纳答案by Yassin Hajaj

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

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
    
  1. 必须由调用者处理

    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. 必须由调用者重新抛出

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

回答by christophetd

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

在您的 main 方法中,对 的调用m16h可能会导致抛出异常。在这种情况下,您有两个选择:

  • handle the exception yourself in the main method.
  • 在 main 方法中自己处理异常。
// 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 Exceptionto its declaration.
  • 通过附加throws Exception到它的声明,表明 main 方法可以抛出异常。

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

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

回答by SCouto

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

使用 try/catch 块包围您调用此方法的行,如下所示:

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());
}