簡體   English   中英

未報告的異常ClassNotFoundException;必須被捕獲或聲明為拋出

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM