为了账号安全,请及时绑定邮箱和手机立即绑定

什么是异常错误,我该如何解决?

什么是异常错误,我该如何解决?

白衣非少年 2022-06-30 18:31:11
我对 Java 非常陌生,需要您的帮助。我的导师给了我 MD5Digest 类,当我自己运行文件时,它编译没有问题。但是当我将类复制并粘贴到我的文件中时,它不起作用。有人可以帮帮我吗?我读到它与 try catch 循环有关,但我不确定如何为这个特定的类设置一个,因为它是给我的,我不知道它的一半是什么意思。提前致谢!import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner;import java.security.MessageDigest;public class Login{    public static void main(String []args){        Scanner user_input = new Scanner(System.in);        String entered_username = Get_Credentials.get_username(user_input);        String entered_password = Get_Credentials.get_password(user_input);        MD5Digest.encrypt(entered_password);        user_input.close();        User[] all_users = File_Scanner.create_users();    }}class Get_Credentials{    public static String get_username(Scanner user_input){        System.out.println("Username: ");        return user_input.next();    }    public static String get_password(Scanner user_input){        System.out.println("Password: ");        return user_input.next();    }}class File_Scanner{    public static User[] create_users(){        User users[] = new User[6];        int index_counter = 0;        try {            File credentials_file = new File("credentials.txt");            String pattern = "[^\"\\s]+|\"(\\\\.|[^\\\\\"])*\"";            Scanner file_reader = new Scanner(credentials_file);            while (file_reader.hasNextLine()) {                users[index_counter] = new User();                users[index_counter].username = file_reader.findInLine(pattern);                users[index_counter].encrypted_password = file_reader.findInLine(pattern);                users[index_counter].password = file_reader.findInLine(pattern);                users[index_counter].role = file_reader.findInLine(pattern);                file_reader.nextLine();                index_counter++;            }            file_reader.close();        }        catch (Exception e) {          System.out.println(e.getClass());        }        return users;    }}
查看完整描述

3 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

你用的是什么IDE?您应该能够轻松解决此错误,而无需任何外部帮助。在 IntelliJ 上,按 Alt + Enter 将为您提供将加密方法调用包含在 try/catch 块中的选项,如下所示:


    try {

        MD5Digest.encrypt(entered_password);

    } catch (Exception e) {

        e.printStackTrace();

    }

该方法会引发异常,这基本上意味着它可能会遇到错误,而 try catch 块是一种处理该错误而不会导致程序崩溃的方法。


encrypt 方法位于 MD5Digest 类的底部。看第一行:


public static void encrypt(String original) throws Exception

它告诉编译器它可能会遇到异常(错误),并且您应该准备好处理这种可能性。因此需要 try/catch。它将尝试 try 括号中的内容,然后如果遇到错误,将执行 catch 块中的代码。


查看完整回答
反对 回复 2022-06-30
?
慕勒3428872

TA贡献1848条经验 获得超6个赞

将您的主要方法更改为: public static void main(String []args) throws Exception {.
由于您是通过方法调用encrypt方法main,因此您还需要将 也添加throws Exceptionmain方法中。希望这可以帮助!

查看完整回答
反对 回复 2022-06-30
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

基本上你只需要给你的方法一个关于可能异常的标志,在这种情况下称为检查异常。因此,您使用MessageDigest抽象类,并且您访问的方法getInstance具有签名为throws NoSuchAlgorithmException. 正如@Dymas 所说,IDE 有助于更清楚地看到它。


所以当encrypt方法使用getInstance方法时,你应该encript知道这个检查的异常。此外,由于main调用encript会带来异常,因此getInstance您还应该main告知这一点。这导致您提出:


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

public static void encrypt(String original) throws NoSuchAlgorithmException

这个页面也很好地解释了它,它可能有助于进一步阅读:


https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/


您可以使用throws NoSuchAlgorithmException或@K.Kretz 所说的签名throws Exception,这是关于异常层次结构,您也可以在这里查看图片:


https://itblackbelt.wordpress.com/2015/02/17/checked-vs-unchecked-exception-in-java-example/


查看完整回答
反对 回复 2022-06-30
  • 3 回答
  • 0 关注
  • 174 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号