英文:
How to fix RuntimeException?
问题
import java.sql.Connection;
public static void main(String[] args) throws SQLException, RuntimeException {
try {
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
String connectionURL = "jdbc:mysql://localhost:3306/aaa?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String login = "root";
String password = "aaa";
try (Connection conn = DriverManager.getConnection(connectionURL, login, password)) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Lista1");
while(rs.next()) {
System.out.println(rs.getString("nazwa"));
}
rs.close();
stmt.close();
}
} catch (SQLException e) {
System.out.println("Blad polaczenia:");
e.printStackTrace();
} catch (RuntimeException e) {
System.out.println("Blad polaczenia:");
e.printStackTrace();
}
}
The console log throws:
Exception in thread "main" java.lang.RuntimeException:
at cccc.web.Main.main(Main.java:1)
I tried to delete return, but it still threw bad issue.
I tried many things but I don't know how to fix it.
I don't know how to solve it. And I spent many many many times to fight with it!!!!
英文:
import java.sql.Connection;
public static void main(String[] args) throws SQLException, RuntimeException {
try{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
String connectionURL="jdbc:mysql://localhost:3306/aaa?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String login="root";
String password="aaa";
try (Connection conn = DriverManager.getConnection(connectionURL, login, password)) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Lista1");
while(rs.next()) {
System.out.println(rs.getString("nazwa"));
}
rs.close();
stmt.close();
}
}
catch(SQLException e) {
System.out.println("Blad polaczenia:");
e.printStackTrace();
}
catch(RuntimeException e) {
System.out.println("Blad polaczenia:");
e.printStackTrace();
}
The console log throws:
Exception in thread "main" java.lang.RuntimeException:
at cccc.web.Main.main(Main.java:1)
I tried to delete return, but it still threw bad issue.
I tried many things but I don't know how to fix it.
I don't know how to solve it. And I spent many many many times to fight with it!!!!
答案1
得分: 1
异常对象包含两个信息,告诉您发生了什么:异常的具体类型(RuntimeException是一个基类),如果幸运的话,会有一个文本消息告诉您问题所在。
在您的异常处理程序中打印它们。
System.out.println("Exception: " + e);
当然,堆栈跟踪会告诉您导致问题的源文件中的确切行。
这实际上不是“修复”异常的问题,而是理解发生了什么,并编写代码来处理这种情况。
英文:
The exception object contains two things that tell you what happened: the specific type of the exception (RuntimeException is a base), and if you're lucky, a text message telling you what the problem is.
Print them in your exception handler.
System.out.println("Exception: " + e)
And, of course, the stacktrace tells you the exact line of your source file that caused the problem.
This is not really a matter of 'fixing' an exception, it's a matter of understanding what is going on and writing code to handle that situation.
答案2
得分: 0
以下是翻译好的内容:
当错误告诉您异常发生在第一行时,这非常像是一个解析问题。请看这里:
try (Connection conn = DriverManager.getConnection(connectionURL, login, password)) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Lista1");
while(rs.next()) {
System.out.println(rs.getString("nazwa"));
}
rs.close();
stmt.close();
}
上述代码是一个没有catch
的try
块。它位于外部的另一个try
中。但是一旦您使用了try
,就需要一个catch
。修复此解析错误,让我们看看是否还有其他问题。
英文:
When an error tells you that the exception happened on the first line, that very much looks like a parsing issue. Take a look here:
try (Connection conn = DriverManager.getConnection(connectionURL, login, password)) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Lista1");
while(rs.next()) {
System.out.println(rs.getString("nazwa"));
}
rs.close();
stmt.close();
}
The code above is a try
without a catch
. It's inside an outer try. But once you are using a try
, you need a catch
. Fix this parsing error and let's see whether there are other problems as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论