第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

什么是異常錯誤,我該如何解決?

什么是異常錯誤,我該如何解決?

白衣非少年 2022-06-30 18:31:11
我對 Java 非常陌生,需要您的幫助。我的導(dǎo)師給了我 MD5Digest 類,當(dāng)我自己運行文件時,它編譯沒有問題。但是當(dāng)我將類復(fù)制并粘貼到我的文件中時,它不起作用。有人可以幫幫我嗎?我讀到它與 try catch 循環(huán)有關(guān),但我不確定如何為這個特定的類設(shè)置一個,因為它是給我的,我不知道它的一半是什么意思。提前致謝!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條經(jīng)驗 獲得超9個贊

你用的是什么IDE?您應(yīng)該能夠輕松解決此錯誤,而無需任何外部幫助。在 IntelliJ 上,按 Alt + Enter 將為您提供將加密方法調(diào)用包含在 try/catch 塊中的選項,如下所示:


    try {

        MD5Digest.encrypt(entered_password);

    } catch (Exception e) {

        e.printStackTrace();

    }

該方法會引發(fā)異常,這基本上意味著它可能會遇到錯誤,而 try catch 塊是一種處理該錯誤而不會導(dǎo)致程序崩潰的方法。


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


public static void encrypt(String original) throws Exception

它告訴編譯器它可能會遇到異常(錯誤),并且您應(yīng)該準備好處理這種可能性。因此需要 try/catch。它將嘗試 try 括號中的內(nèi)容,然后如果遇到錯誤,將執(zhí)行 catch 塊中的代碼。


查看完整回答
反對 回復(fù) 2022-06-30
?
慕勒3428872

TA貢獻1848條經(jīng)驗 獲得超6個贊

將您的主要方法更改為: public static void main(String []args) throws Exception {.
由于您是通過方法調(diào)用encrypt方法main,因此您還需要將 也添加throws Exceptionmain方法中。希望這可以幫助!

查看完整回答
反對 回復(fù) 2022-06-30
?
肥皂起泡泡

TA貢獻1829條經(jīng)驗 獲得超6個贊

基本上你只需要給你的方法一個關(guān)于可能異常的標志,在這種情況下稱為檢查異常。因此,您使用MessageDigest抽象類,并且您訪問的方法getInstance具有簽名為throws NoSuchAlgorithmException. 正如@Dymas 所說,IDE 有助于更清楚地看到它。


所以當(dāng)encrypt方法使用getInstance方法時,你應(yīng)該encript知道這個檢查的異常。此外,由于main調(diào)用encript會帶來異常,因此getInstance您還應(yīng)該main告知這一點。這導(dǎo)致您提出:


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,這是關(guān)于異常層次結(jié)構(gòu),您也可以在這里查看圖片:


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


查看完整回答
反對 回復(fù) 2022-06-30
  • 3 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號