[英]error unreported exception ClassNotFoundException; must be caught or declared to be thrown
[英]unreported exception ClassNotFoundException;must be caught or declared to be thrown
這是我嘗試使用jdk進行編譯的程序。程序中的“ satya”指的是msaccess db文件數據庫。當我嘗試對其進行編譯時,它顯示諸如"MyClass.java:0:error:unreported exception ClassNotFoundException;must be caught or declared to be thrown"
類的錯誤"MyClass.java:0:error:unreported exception ClassNotFoundException;must be caught or declared to be thrown"
如果我更改了計划外的。連SQLException
到異常它編譯sucessfully.But運行program.How執行時拋出異常?
import java.sql.*;
class MyClass
{
public static void main(String args[])
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:satya","","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from studentinfo");
while(rs.next())
{
System.out.println(rs.getInt(1)+"\t"+
rs.getString(2)+"\t"+
rs.getString(3)+"\t");
}
rs.close();
st.close();
}
catch (SQLException e) {
System.out.println("<P>" + "There was an error doing the query:");
System.out.println ("<PRE>" + e + "</PRE> \n <P>");
}
}
}
您的方法之一會引發ClassNotFoundException
並且您有責任處理這些異常。 快速修復
public static void main(String args[]) throws Exception
在這種情況下,異常會輸出到您的控制台(請使用該輸出擴展您的問題)。
此外,請確保將jdbc驅動程序庫添加到項目中。
ClassNotFoundException
是Exception的子類,這就是為什么要傳遞編譯器的原因。 SQLException
類不包裝ClassNotFoundException
。 由於“ Class
”類的forName()
方法被聲明為“ throws ClassNotFoundException
”,因此編譯器將要求您在調用方方法上用try / catch塊或throws
子句包裝方法調用。 catch
塊必須使用ClassNotFoundException
或其父Exception
類之一。
您需要找出在運行時引發的異常,以檢查其為何無法提前運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.