java中的異常

java異常

? Notice that all exceptions descend from Throwable, but the hierarchy immediately splits into two branches: Error and Exception.

? The Error hierarchy describes internal errors and resource exhaustion situations inside the Java runtime system. You should not throw an object of this type. There is little you can do if such an internal error occurs, beyond notifying the user and trying to terminate the program gracefully. These situations are quite rare.

When doing Java programming, you focus on the Exception hierarchy. The Exception hierarchy also splits into two branches: exceptions that derive from RuntimeException and those that do not.

The general rule is this: A RuntimeException happens because you made a programming error. Any other exception occurs because a bad thing, such as an I/O error, happened to your otherwise good program.

Exceptions that inherit from RuntimeException include such problems as

? A bad cast

? An out-of-bounds array access

? A null pointer access

Exceptions that do not inherit from RuntimeException include

? Trying to read past the end of a file

? Trying to open a file that doesn’t exist

? Trying to find a Class object for a string that does not denote an existing class

? The rule “If it is a RuntimeException, it was your fault” works pretty well. You could have avoided that ArrayIndexOutOfBoundsException by testing the array index against the array bounds. The NullPointerException would not have happened had you checked whether the variable was null before using it.

? How about a file that doesn’t exist? Can’t you first check whether the file exists, and then open it? Well, the file might be deleted right after you checked for its existence. Thus, the notion of “existence” depends on the environment, not just on your code.

? The Java Language Specification calls any exception that derives from the class Error or the class?RuntimeException an unchecked exception. All other exceptions are called checked exceptions. This is useful terminology that we also adopt. The compiler checks that you provide exception handlers for all checked exceptions.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市谍咆,隨后出現(xiàn)的幾起案子敦腔,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異苍蔬,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)稀并,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門仅颇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人碘举,你說(shuō)我怎么就攤上這事忘瓦。” “怎么了引颈?”我有些...
    開封第一講書人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵耕皮,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我蝙场,道長(zhǎng)凌停,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任售滤,我火速辦了婚禮罚拟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘完箩。我一直安慰自己赐俗,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開白布弊知。 她就那樣靜靜地躺著阻逮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪秩彤。 梳的紋絲不亂的頭發(fā)上叔扼,一...
    開封第一講書人閱讀 49,792評(píng)論 1 290
  • 那天事哭,我揣著相機(jī)與錄音,去河邊找鬼币励。 笑死慷蠕,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的食呻。 我是一名探鬼主播流炕,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼仅胞!你這毒婦竟也來(lái)了每辟?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤干旧,失蹤者是張志新(化名)和其女友劉穎渠欺,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體椎眯,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡挠将,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了编整。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片舔稀。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖掌测,靈堂內(nèi)的尸體忽然破棺而出内贮,到底是詐尸還是另有隱情,我是刑警寧澤汞斧,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布夜郁,位于F島的核電站,受9級(jí)特大地震影響粘勒,放射性物質(zhì)發(fā)生泄漏竞端。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一庙睡、第九天 我趴在偏房一處隱蔽的房頂上張望婶熬。 院中可真熱鬧,春花似錦埃撵、人聲如沸赵颅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)饺谬。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間募寨,已是汗流浹背族展。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拔鹰,地道東北人仪缸。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像列肢,于是被迫代替她去往敵國(guó)和親恰画。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,309評(píng)論 0 10
  • 針對(duì)兒子鉆空子的毛病,今天我反思了好多欧聘。 家長(zhǎng)不一致的教育理念,給孩子“愛鉆空子”充當(dāng)了“保護(hù)...
    niuniubaobei521閱讀 82評(píng)論 0 1
  • 今天晴上午去海邊看了一下海片林,腳下踩著松軟軟的沙灘,吹著伶亂的海風(fēng)怀骤,上面飛著幾只海歐费封,遠(yuǎn)處有一條大船,下午開始開活蒋伦。
    湯秋元閱讀 84評(píng)論 0 0
  • 小假期早結(jié)束了孝偎,假期前寫的計(jì)劃完成多少了?完成一半了凉敲!進(jìn)度很慢,慢的我自己受不了寺旺。原以為有大塊時(shí)間讀書爷抓,可以肆無(wú)...
    Ply坤閱讀 725評(píng)論 0 0
  • 第七期主題拆書訓(xùn)練營(yíng)《好好說(shuō)話》第3次作業(yè) 掀桌砍價(jià)三步法 by 火龍果隊(duì)隊(duì)長(zhǎng)郭駿豪 I:重述知識(shí) 購(gòu)...
    駿豪John閱讀 375評(píng)論 0 1