<ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>


      背景故事

      ?


      影片《金剛》是2005年上映的一部冒險(xiǎn)電影,它講述1933年的美國,一名勇于冒險(xiǎn)的企業(yè)家及電影制作者,率領(lǐng)攝制隊(duì)伍到荒島拍攝,其中包括女主角安及編劇杰克,他們遇到恐龍及當(dāng)?shù)赝林囊u擊,安發(fā)出的尖叫聲換來金剛的回應(yīng)。這只巨大無比的猩猩,連兇悍的恐龍也懼怕它幾分,偏偏它卻鐘情于安。安其后將金剛由荒島帶回紐約,但卻是它悲劇命運(yùn)的開始。后來金剛被抓到了城市。為保護(hù)愛人同軍隊(duì)?wèi)?zhàn)斗,金剛為了帶安再看一次她曾說過美麗的日出,爬上了帝國大廈,使自己陷入困境,與人類的飛機(jī)展開了最后決戰(zhàn)。最后它摔下了帝國大廈,為自己的愛人譜寫了最后的悲歌。

      try-catch之一矮矬窮泡妞

      java程序猿也會(huì)碰到難以琢磨的try-catch,請(qǐng)看下面的例子
      public static void main(String[] args) { try { System.out.println("Hello world"
      ); }catch(IOException e) { System.out.println("抓到一個(gè)IO 異常!"); } }
      ?

      這段代碼可以打印出什么?

      可能不少人會(huì)說,這個(gè)不是很簡單嘛?打印出:

      Hello world

      其實(shí)它壓根編譯都不能通過

      ?

      報(bào)錯(cuò)情況

      Unreachable catch block for IOException. This exception is never thrown from
      the try statement body

      簡單的說,就是try里沒有能拋出IOException異常的語句,catch該異常就通不過編譯。

      JSL-11.2.3里規(guī)定了
      It is a compile-time error if a method or constructor body can throw some
      exception class E when E is a checked exception class and E is not a subclass
      of some class declared in the throws clause of the method or constructor. It is
      a compile-time error if a lambda body can throw some exception class E when E
      is a checked exception class and E is not a subclass of some class declared in
      the throws clause of the function type targeted by the lambda expression. It is
      a compile-time error if a class variable initializer (§8.3.2) or static
      initializer (§8.7) of a named class or interface can throw a checked exception
      class. It is a compile-time error if an instance variable initializer (§8.3.2)
      or instance initializer (§8.6) of a named class can throw a checked exception
      class, unless the named class has at least one explicitly declared constructor
      and the exception class or one of its superclasses is explicitly declared in
      the throws clause of each constructor. It is a compile-time error if a catch
      clause can catch checked exception class E1 and it is not the case that the try
      block corresponding to the catch clause can throw a checked exception class
      that is a subclass or superclass of E1, unless E1 is Exception or a superclass
      of Exception. It is a compile-time error if a catch clause can catch an
      exception class E1 and a preceding catch clause of the immediately enclosing
      try statement can catch E1 or a superclass of E1.
      ?

      根據(jù)上面所述,矮矬窮泡妞本身都被排除掉了,只有有一項(xiàng)特長,才能泡妞!

      最簡單的方法是拋出一個(gè)異常或者子異常
      import java.io.IOException; public class TryCatchException { public static void
      main(String[] args) {try { System.out.println("Hello world"); throw new
      IOException();//或者子異常,如throw new FileNotFoundException(); } catch(IOException
      e) { System.out.println("抓到一個(gè)IO 異常!"); } } }
      ?

      try-catch之二高富帥泡妞

      那來看看這個(gè)吧!打印什么?
      public class TryCatchException { public static void main(String[] args) { try
      { System.out.println("hello world!"); } catch(Exception e) { System.out.println(
      "捕獲到異常"); } } }
      ?

      可能不少人會(huì)說,不是和上面的一樣嘛!會(huì)報(bào)編譯異常。

      ?

      哈哈,你掉到坑里了!它打印
      hello world!
      不管與其相對(duì)應(yīng)的try子句的內(nèi)容是什么,捕獲Exception或者Throwable的catch語句是ok的,這點(diǎn)jsl并沒有說清楚。

      總之,高富帥泡妞總是很超脫的,很多妞也愿意倒撲!

      try-catch之三泡妞技可以繼承嗎?

      我們來看看異常如何繼承的吧
      public interface FileNotFound { void run() throws FileNotFoundException; }
      public interface CloneNotSupported { void run() throws
      CloneNotSupportedException; }public class TryCatchException implements
      FileNotFound,CloneNotSupported {public static void main(String[] args) {
      TryCatchException e=new TryCatchException(); e.run(); } @Override public void
      run() { System.out.println("Hello world"); } }
      ?




      上面的程序可以編譯通過嗎?不少人會(huì)說,不能通過編譯!原因:TryCatchException繼承了FileNotFound和CloneNotSupported的方法,同事必然繼承它的異常,故必須捕獲這兩個(gè)異常。

      ?

      你再次避過了正確答案!可以正確編譯,且打印出結(jié)果。

      一個(gè)方法可以拋出的受檢查異常的集合時(shí)它所適用所有類型聲明要拋出的受檢查異常的交集,而不是合集。

      小結(jié):

      矮矬窮就別想著捕獲少女心了,想著一項(xiàng)特長吧,比如富!

      高富帥可以無所顧忌,無往不利。

      泡妞只能最新化的繼承,不用太擔(dān)心。

      參考資料

      【1】java解惑

      友情鏈接
      ioDraw流程圖
      API參考文檔
      OK工具箱
      云服務(wù)器優(yōu)惠
      阿里云優(yōu)惠券
      騰訊云優(yōu)惠券
      京東云優(yōu)惠券
      站點(diǎn)信息
      問題反饋
      郵箱:[email protected]
      QQ群:637538335
      關(guān)注微信

        <ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>
          国产黄色视频免费看 | 人伦片无码中文字幕麻豆 | 国产黄色在线看 | www.怡红院 | 破處女黃色一級片 | 黄片免费网址 | 久久久99国产精品免费 | 开心五月亚洲综合 | 日韩成人精品在线观看 | 夜夜骚视频 |