[英]Java Errors: Exception in thread “main” java.lang.RuntimeException
[英]Error Exception in thread "main" java.lang.RuntimeException
我試圖修復錯誤,但它總是返回此異常,並且在嘗試處理時異常仍然出錯
終端異常
$ /usr/bin/env c:\\Users\\lolo2\\.vscode\\extensions\\vscjava.vscode-java-debug-0.34.0\\scripts\\launcher.bat "C:\\Program Files\\Java\\jdk-15.0.2\\bin\\java.exe" -XX:+ShowCodeDetailsInExceptionMessages -Dfile.encoding=UTF-8 @C:\\Users\\lolo2\\AppData\\Local\\Temp\\cp_60g01ctldkies4ai3k17s3gfy.argfile br.com.efipee.exceptions.AlunosMain
Aluno 1
Aluna 2
Aluno 3
Aluno 4
Aluno 5
Palavra 6
Exception in thread "main" java.lang.RuntimeException: Arquivo não encontrado
at br.com.efipee.exceptions.AlunoService.findAll(AlunoService.java:44)
at br.com.efipee.exceptions.AlunoService.findAluno(AlunoService.java:49)
at br.com.efipee.exceptions.AlunosMain.main(AlunosMain.java:7)
AlunosMain.java
package br.com.efipee.exceptions;
public class AlunosMain {
public static void main(String[] args) {
try {
new AlunoService().findAluno("Palavra 6");
} catch (AlunoNaoExisteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
AlunoService.java
package br.com.efipee.exceptions;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
public class AlunoService {
public List<String> findAll() {
// recebendo caminho arquivo
File file = new File("C:\\Users\\lolo2\\OneDrive\\Documentos\\JavaPro\\Modulo\\exceptions\\target\\classes\\alunos.txt");
//C:\\Users\\lolo2\\OneDrive\\Documentos\\JavaPro\\Modulo\\exceptions\\target\\classes\\
//instanciando Scanner
Scanner sc = null;
// tratamento errro
try {
// lendo arquivo
sc = new Scanner(file);
// procurando linhas
while (sc.hasNextLine()) {
// imprimindo linhas
System.out.println(sc.nextLine());
}// try
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}//cath
// fechando Scanner
finally {
if(sc != null){
sc.close();
}//if
}//finally
throw new RuntimeException("Arquivo não encontrado");
}// findAll
public String findAluno(String nome) throws AlunoNaoExisteException{
return findAll().stream()
.filter(aluno -> aluno.equals(nome))
.findFirst()
.orElseThrow(() -> new AlunoNaoExisteException(nome));
}
}// class
這個之前的代碼也有問題。
舊 AlunoService.java
public List<String> findAlll(){
String caminho = AlunoService.class.getClassLoader()
.getResource("alunos.txt")
.getPath();
try{
List<String> alunos = Files.readAllLines(Path.of(caminho));
return alunos;
}catch(IOException ioException){
return Collections.emptyList();
}
控制台上返回以下內容:“Illegal char <:> at index 2:”
當您執行findAll().stream()
時,“findAll”方法需要非異常完成。
沒有返回列表的代碼路徑?
而不是throw new RuntimeException(...);
利用
throw new AlunoNaoExisteException(...)
那么它應該被抓住。 或者捕獲一個 RuntimeException。
也許你想修復findAll
public List<String> findAll() {
List<String> results = new ArrayList<>();
... //your code
// replace this
//System.out.println(sc.nextLine());
//with this.
results.add(sc.nextLine());
... //finish.
return results;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.