2016-03-19 5 views
1

私はDatabaseControlクラスを持っています。 Database classは、SQLiteデータベースを作成し、Dataなどを挿入するためのメソッドを提供することにより、SQLiteデータベースへのアクセスを容易にします。これらのメソッドは、すべてのメソッドで処理されるSqlJetExceptionをスローしている可能性があります。キャッチされたSqlJetExceptionを持つメソッドは、実行後にSqlJetExceptionをスローします

Control classDatabase classからこれらのメソッドを呼び出していますが、SqlJetExceptionがキャッチされていないと訴えています。私は個人的には、オブジェクト指向の悪い設計としてControl classのSqlJetExceptionの処理を見積もっているので、私はそれを避けたいと思います。

私の質問なぜ私のデータベースメソッドは例外をスローしますか?そして私はそれをどのように修正できますか?

エラーログ

[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ voc --- 
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! 
[INFO] Compiling 2 source files to /home/toogley/src/voc/target/classes 
[INFO] ------------------------------------------------------------- 
[ERROR] COMPILATION ERROR : 
[INFO] ------------------------------------------------------------- 
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[14,23] error: unreported exception SqlJetException; must be caught or declared to be thrown 
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[15,25] error: unreported exception SqlJetException; must be caught or declared to be thrown 
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[16,21] error: unreported exception SqlJetException; must be caught or declared to be thrown 
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[20,21] error: unreported exception SqlJetException; must be caught or declared to be thrown 
[INFO] 4 errors 
[INFO] ------------------------------------------------------------- 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 

データベースクラス:

package voc; 

import org.tmatesoft.sqljet.core.*; 
import org.tmatesoft.sqljet.core.table.ISqlJetTable; 
import org.tmatesoft.sqljet.core.table.SqlJetDb; 

import java.io.File; 

public class Database { 
    private SqlJetDb db; 

    public void createNew() throws SqlJetException { 
     File dbFile = new File("db.sqlite"); 
     dbFile.delete(); 

     try { 
      db = SqlJetDb.open(dbFile, true); 
      db.getOptions().setAutovacuum(true); 
      db.beginTransaction(SqlJetTransactionMode.WRITE); 

      db.getOptions().setUserVersion(1); 
     } catch (SqlJetException e) { 
      e.printStackTrace(); 
     } finally { 
      db.commit(); 
     } 
    } 

    public void createTable() throws SqlJetException { 
     try { 
      db.beginTransaction(SqlJetTransactionMode.WRITE); 

      db.createTable("voc"); 
      db.createIndex("english-term"); 
      db.createIndex("german-translation"); 

     } catch (SqlJetException e) { 
      e.printStackTrace(); 
     } finally { 
      db.commit(); 
     } 
    } 

    public void setData(String englishVoc, String germanTranslation) throws SqlJetException { 
     try { 
      db.beginTransaction(SqlJetTransactionMode.WRITE); 

      ISqlJetTable table = db.getTable("voc"); 
      table.insert(englishVoc, germanTranslation); 

     } catch (SqlJetException e) { 
      e.printStackTrace(); 
     } finally { 
      db.commit(); 
     } 
    } 

    public String getData(String s) throws SqlJetException { 
     String returnVal = ""; 
     try { 
      db.beginTransaction(SqlJetTransactionMode.READ_ONLY); 

      ISqlJetTable table = db.getTable("voc"); 
      returnVal = table.lookup(s).toString(); 
     } catch (SqlJetException e) { 
      e.printStackTrace(); 
     } finally { 
      db.commit(); 
     } 
    return returnVal; 
    } 

コントロールクラス

package voc; 

public class Control { 

    private Database theDB; 

    public Control() { 
     theDB = new Database(); 
    } 

    public void fillDB() { 
     theDB.createNew(); // <<< SqlJetException 
     theDB.createTable(); // <<< SqlJetException 
     theDB.setData("mobility", "Ortsunabhängigkeit"); // <<< SqlJetException 
    } 

    public void receiveData() { 
     theDB.getData("test"); // <<< SqlJetException 
    } 
} 

答えて

1

あなたはCATC場合それらのすべてが、あなたのメソッドがSqlJetExceptionをスローすることはもはや真実ではなく、メソッドシグネチャのその部分を削除する必要があります。

コンパイラはこのような状況を実際には理解していません。メソッドがこれをスローして例外をチェックすると宣言すると、コンパイラはそれが真であるかどうかをチェックしません。あなたのコードが実際にチェックされた例外をスローする可能性があり、メソッドシグニチャでキャッチも宣言もされていない場合、コンパイラからエラーメッセージが返されます。

関連する問題