Catch IO Exception未执行

时间:2016-03-23 23:56:07

标签: java netbeans

抛出异常时,不会执行catch块内的代码。我不确定异常是否被抛出我想的地方。有什么想法吗? 这是输出:

线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException

package de.jeanma.stackOverflow;

import java.lang.Integer;
import java.lang.String;

public class Transactions{
    private String date, time, category, value, tags, description;
    private int payee, payer;

    public Transactions(){
    //Add everything here, needed for the constructor
    }
    public Transactions(String date, String time, String category, int payee, //I recommend creating an constructor that sets the values as well, beacause you
        int payer, String value, String tags, String Description){ // might want to create the object and call the save() directly withoud calling every setter one by one          
        this.date = date;
        this.time = time;
        this.category = category;
        this.value = value;
        this.tags = tags;
        this.description = description;
        this.payee = payee;
        this.payer = payer;
        //Add everything here, needed for the constructor as well
    }

    //Here you can place all your other methods

    public void save(){
        if(!(isInitialized(date) && isInitialized(time) && isInitialized(category) && isInitialized(value) //here all values are checked if they are initialized. 
            && isInitialized(tags) && isInitialized(description) && isInitialized(Integer.valueOf(payee)) //The primitive int's are made to complex Integer's because 
            && isInitialized(Integer.valueOf(payer)))){                                                 // the method isInitialized() expects an object 
        //here you could throw an exception or do something like: System.exit(-1);
        }
    }
    private boolean isInitialized(Object Obj){ // this is the method that's checking if the value is initialized
        if(Obj.equals(null)) return false;
        else return true;
    }

    //Add all the setters here (I'm too lazy to do that now)

}

3 个答案:

答案 0 :(得分:2)

NullPointerException不是IOException

答案 1 :(得分:2)

同样更新你的代码,

try {
     ...   
   }    
catch (IOException ex) {
    ...
    }
catch (NullPointerException ex) {
    showError("NullPointerException catch");
    System.out.println("Inside NullPointerException-Catch Block");  
    }
finally{
    ....
    }

答案 2 :(得分:1)

你的catch只会捕获IOException。如果要捕获所有异常,请使用

catch (Exception ex) {
    showError("Picture not fully received");
    System.out.println("Inside Catch Block");  
}
相关问题