在捕获到IOException以后, 可以进行再次包装. 例如 try { // some operation about FileReader } catch (IOException e) { throw new MyException(e); }class MyException extends Exception { public MyException(Throwable t) { super(t); } } 再次包装以后与IOException有没有继承关系都无所谓了.
我创建了一个异常FancySrcException,代码如下: package fancysrcframe;import java.io.IOException;public class FancySrcException extends IOException { public FancySrcException() { } public FancySrcException(String jljlj){ super(jljlj); } }package fancysrcframe;import java.io.*; import java.awt.*;class FancySrcCanvas extends Canvas { FancySrcCanvas() { /* the content of the constructor */ } // some method private void paintText(Graphics g) { try { FileReader fr = new FileReader(fileName); LineNumberReader lnr = new LineNumberReader(fr); // .... lnr.close(); fr.close(); } catch (FancySrcException x) { System.out.println("Trouble!" + x.getMessage()); } } }
我创建了一个异常FancySrcException,代码如下: package fancysrcframe;import java.io.IOException;public class FancySrcException extends IOException { public FancySrcException() { } public FancySrcException(String jljlj){ super(jljlj); } }package fancysrcframe;import java.io.*; import java.awt.*;class FancySrcCanvas extends Canvas { FancySrcCanvas() { /* the content of the constructor */ } // some method private void paintText(Graphics g) { try { FileReader fr = new FileReader(fileName); LineNumberReader lnr = new LineNumberReader(fr); // .... lnr.close(); fr.close(); } catch (FancySrcException x) { System.out.println("Trouble!" + x.getMessage()); } } }但每次运行度出现如下异常:"FancySrcCanvas.java": unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown at line 57, column 23 "FancySrcCanvas.java": unreported exception java.io.IOException; must be caught or declared to be thrown at line 62, column 17 "FancySrcCanvas.java": unreported exception java.io.IOException; must be caught or declared to be thrown at line 69, column 11何解????
try {
// some operation about FileReader
} catch (IOException e) {
throw new MyException(e);
}class MyException extends Exception {
public MyException(Throwable t) {
super(t);
}
}
再次包装以后与IOException有没有继承关系都无所谓了.
package fancysrcframe;import java.io.IOException;public class FancySrcException extends IOException {
public FancySrcException() {
}
public FancySrcException(String jljlj){
super(jljlj);
}
}package fancysrcframe;import java.io.*;
import java.awt.*;class FancySrcCanvas
extends Canvas {
FancySrcCanvas() {
/* the content of the constructor
*/
} // some method private void paintText(Graphics g) {
try {
FileReader fr = new FileReader(fileName);
LineNumberReader lnr = new LineNumberReader(fr);
// ....
lnr.close();
fr.close();
}
catch (FancySrcException x) {
System.out.println("Trouble!" + x.getMessage());
}
} }
package fancysrcframe;import java.io.IOException;public class FancySrcException extends IOException {
public FancySrcException() {
}
public FancySrcException(String jljlj){
super(jljlj);
}
}package fancysrcframe;import java.io.*;
import java.awt.*;class FancySrcCanvas
extends Canvas {
FancySrcCanvas() {
/* the content of the constructor
*/
} // some method private void paintText(Graphics g) {
try {
FileReader fr = new FileReader(fileName);
LineNumberReader lnr = new LineNumberReader(fr);
// ....
lnr.close();
fr.close();
}
catch (FancySrcException x) {
System.out.println("Trouble!" + x.getMessage());
}
} }但每次运行度出现如下异常:"FancySrcCanvas.java": unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown at line 57, column 23
"FancySrcCanvas.java": unreported exception java.io.IOException; must be caught or declared to be thrown at line 62, column 17
"FancySrcCanvas.java": unreported exception java.io.IOException; must be caught or declared to be thrown at line 69, column 11何解????
1。FileReader fr = new FileReader(fileName);
要求捕捉FileNotFoundException,而你没有
2。lnr.close(); fr.close();
都要求捕捉IOException,而你的FancySrcException虽然继承自IOException,去不能代替IOException,就像男人继承自人,但男人却不能代替人一样。
改成这样就没问题了:
try {
FileReader fr = new FileReader("d:\\a.txt");
LineNumberReader lnr = new LineNumberReader(fr);
// ....
lnr.close();
fr.close();
}
catch (FancySrcException x1) {
System.out.println("Trouble1!");
}
catch (FileNotFoundException x2) {
System.out.println("Trouble2!");
}
catch (IOException x3) {
System.out.println("Trouble3!");
}