未报告的异常java.lang.Exception;必须被抓住或宣布被抛出 [英] Unreported exception java.lang.Exception; must be caught or declared to be thrown

查看:110
本文介绍了未报告的异常java.lang.Exception;必须被抓住或宣布被抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried compiling the below but get the following around 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;
    }
}


推荐答案

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

方法的签名表示异常很容易被抛出。

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

这意味着例外:


  1. 必须由来电者处理

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


  • 调用者必须重新抛出

  • Must be rethrowed by the caller

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


  • 这篇关于未报告的异常java.lang.Exception;必须被抓住或宣布被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆