[英]Why don't I see code when viewing included Jar libraries in Eclipse?
[英]Eclipse. Why don't I see compile error if method with unreachable code doesn't invoke?
我注意到如果沒有無法訪問的代碼的方法沒有調用 - 那么這個代碼由eclipse編譯器編譯並執行。
示范:
import java.io.FileNotFoundException;
class R3 {
public void g(){
try {
} catch (FileNotFoundException e) {//any checked exception
}
}
public static void main(String [] args) {
System.out.println("23");
new R3().g();
}
}
結果:
Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body
並將其與以下代碼進行比較:
import java.io.FileNotFoundException;
class R3 {
public void g(){
try {
} catch (FileNotFoundException e) {//any checked exception
}
}
public static void main(String [] args) {
System.out.println("23");
//new R3().g();
}
}
編譯並正常執行。
它是eclipse編譯器優化還是正常行為?
這可能有助於討論。 當我用eclipse編譯它然后在網上反編譯它時,這就是我得到的。 出於某種原因,Eclipse決定編譯您的方法以拋出運行時錯誤,即存在未解決的編譯問題? 有趣。
class R3
{
public void g()
{
throw new Error("Unresolved compilation problem: \n\tUnreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body\n");
}
public static void main(String[] args)
{
System.out.println("23");
}
}
我在SO的其他地方發現了這個,但它解釋了發生得非常清楚的事情。
一個值得注意的區別是Eclipse編譯器允許您運行實際上沒有正確編譯的代碼。 如果從未運行帶有錯誤的代碼塊,則程序運行正常。 否則,它將拋出一個異常,表明您嘗試運行無法編譯的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.