1.javaä¸å¸¸è§çå¼å¸¸ç±»
2.throwableåExceptionçåºå«(详ç»ä¸ç¹)
3.exception和error区别
4.javaä¸ å¼å¸¸ç±»Exception(String message, Throwable cause)
5.exceptionåerrorçåºå«
javaä¸å¸¸è§çå¼å¸¸ç±»
ä»å¼å¸¸ç±»ç继æ¿æ¶æå¾ä¸å¯ä»¥çåºï¼Exception ç±»æ©å±åºæ°ä¸ªåç±»ï¼å ¶ä¸ IOExceptionãRunntimeException æ¯è¾å¸¸ç¨ç两ç§ãä¹ æ¯ä¸å° Error ä¸ Exception ç±»ç»ç§°ä¸ºå¼å¸¸ç±»ï¼ä½è¿ä¸¤è æ¬è´¨ä¸è¿æ¯æä¸åçãError ç±»ä¸é¨ç¨æ¥å¤ç严éå½±åç¨åºè¿è¡çé误ï¼å¯æ¯é常ç¨åºè®¾è®¡è ä¸ä¼è®¾è®¡ç¨åºä»£ç å»ææè¿ç§é误ï¼å ¶åå å¨äºå³ä½¿ææå°å®ï¼ä¹æ æ³ç»äºéå½çå¤çï¼å¦ JAVA èææºåºéå°±å±äºä¸ç§ Errorã
ä¸åäº Error ç±»ï¼Exception ç±»å å«äºä¸è¬æ§çå¼å¸¸ï¼è¿äºå¼å¸¸é常å¨ææå°ä¹å便å¯å妥åçå¤çï¼ä»¥ç¡®ä¿ç¨åºç»§ç»è¿è¡ï¼å¦ TestException7_2 éæææå°ç ArrayIndexOutOfBoundsException å°±æ¯å±äºè¿ç§å¼å¸¸ã
RunntimeException å³ä½¿ä¸ç¼åå¼å¸¸å¤ççç¨åºä»£ç ï¼ä¾ç¶å¯ä»¥ç¼è¯æåï¼èè¿ç§å¼å¸¸å¿ é¡»æ¯å¨ç¨åºè¿è¡æ¶ææå¯è½åçï¼ä¾å¦ï¼æ°ç»çç´¢å¼å¼è¶ åºäºèå´ã
ä¸RunntimeException ä¸åçæ¯ï¼IOException ä¸å®è¦ç¼åå¼å¸¸å¤ççç¨åºä»£ç æè¡ï¼å®é常ç¨æ¥å¤çä¸è¾å ¥/è¾åºç¸å ³çæä½ï¼å¦æ件ç访é®ãç½ç»çè¿æ¥çã
å½å¼å¸¸åçæ¶ï¼åçå¼å¸¸çè¯å¥ä»£ç ä¼æåºä¸ä¸ªå¼å¸¸ç±»çå®ä¾å对象ï¼ä¹åæ¤å¯¹è±¡ä¸ catch è¯å¥ä¸çç±»çç±»åè¿è¡å¹é ï¼ç¶åå¨ç¸åºç catch ä¸è¿è¡å¤çã
æ©å±èµæï¼
Javaå¼å¸¸çåç±»ï¼
Javaæ å裤å 建äºä¸äºéç¨çå¼å¸¸ï¼è¿äºç±»ä»¥Throwable为顶å±ç¶ç±»ã
Throwableåæ´¾çåºErrorç±»åExceptionç±»ã
é误ï¼Error类以åä»çåç±»çå®ä¾ï¼ä»£è¡¨äºJVMæ¬èº«çé误ãé误ä¸è½è¢«ç¨åºåéè¿ä»£ç å¤çï¼Errorå¾å°åºç°ãå æ¤ï¼ç¨åºååºè¯¥å ³æ³¨Exception为ç¶ç±»çåæ¯ä¸çåç§å¼å¸¸ç±»ã
å¼å¸¸ï¼Exception以åä»çåç±»ï¼ä»£è¡¨ç¨åºè¿è¡æ¶åéçåç§ä¸ææåççäºä»¶ãå¯ä»¥è¢«Javaå¼å¸¸å¤çæºå¶ä½¿ç¨ï¼æ¯å¼å¸¸å¤ççæ ¸å¿ã
åèèµæï¼ç¾åº¦ç¾ç§-å¼å¸¸
throwableåExceptionçåºå«(详ç»ä¸ç¹)
catch
n. ææ,é·é±,æè·ç©
v. æä½ï¼èµ¶ä¸,äºè§£,ææ
Throwable å¯ææ·ç
exception n. ä¾å¤
Please read:
/bbs/dispbbs.asp?boardid=&ID=&replyID=
Javaè¯è¨è¦æ±javaç¨åºä¸ï¼æ 论æ¯è°åç代ç ï¼æææåºï¼throwï¼çå¼å¸¸é½å¿ é¡»æ¯ä»Throwableæ´¾çèæ¥ãå½ç¶ï¼å®é çJavaç¼ç¨ä¸ï¼ç±äºJDKå¹³å°å·²ç»ä¸ºæ们设计好äºé常丰å¯åå®æ´çå¼å¸¸å¯¹è±¡å类模åãå æ¤ï¼javaç¨åºåä¸è¬æ¯ä¸éè¦åéæ°å®ä¹èªå·±çå¼å¸¸å¯¹è±¡ãèä¸å³ä¾¿æ¯éè¦æ©å±èªå®ä¹çå¼å¸¸å¯¹è±¡ï¼ä¹å¾å¾ä¼ä»Exceptionæ´¾çèæ¥ãæ以ï¼å¯¹äºjavaç¨åºåèè¨ï¼å®ä¸è¬åªéè¦å¨å®ç顶级å½æ°ä¸catch(Exception ex)å°±å¯ä»¥æè·åºææçå¼å¸¸å¯¹è±¡ã ææå¼å¸¸å¯¹è±¡çæ ¹åºç±»æ¯ Throwable ï¼Throwableä»Objectç´æ¥ç»§æ¿èæ¥ï¼è¿æ¯javaç³»ç»æ强å¶è¦æ±çï¼ï¼å¹¶ä¸å®å®ç°äº Serializableæ¥å£ï¼è¿ä¸ºææçå¼å¸¸å¯¹è±¡é½è½å¤è½»æ¾è·¨è¶Javaç»ä»¶ç³»ç»å好äºæå åçç©è´¨åå¤ï¼ãä»Throwableç´æ¥æ´¾çåºçå¼å¸¸ç±»æExceptionåError ãExceptionæ¯javaç¨åºåææçæçï¼å®ä¸è¬ä»£è¡¨äºçæ£å®é æä¹ä¸çå¼å¸¸å¯¹è±¡çæ ¹åºç±»ãä¹å³æ¯è¯´ï¼ Exception åä»å®æ´¾çèæ¥çææå¼å¸¸é½æ¯åºç¨ç¨åºè½å¤catchå°çï¼å¹¶ä¸å¯ä»¥è¿è¡å¼å¸¸é误æ¢å¤å¤ççå¼å¸¸ç±»åãèErrorå表示Javaç³»ç»ä¸åºç°äºä¸ä¸ªé常严éçå¼å¸¸é误ï¼å¹¶ä¸è¿ä¸ªé误å¯è½æ¯åºç¨ç¨åºæä¸è½æ¢å¤çï¼ä¾å¦LinkageError ï¼æ ThreadDeath çã
exception和error区别
以java为例,其exception和error的区别如下:
1、Exception和Error都继承了Throwable类,在java中只有Throwable类型的闲置交友APp源码实例才可以被抛出(Throw)或者捕捉(catch),它是异常处理机制的基本组成类型。Exception是协议清粉源码程序正常运行中,可以预料的意外情况,可能并且应该被捕获,进行相应的处理。
2、Error指在正常情况下,不大可能出现的情况,绝大部门的py获取网页源码Error都会导致程序处于非正常的,不可恢复状态。既然是非正常情况,所以不便于也不需要捕获。贷款app制作源码比如常见的OutOfMemoryError之类,都是Error的子类。
Exception类又分为可检查异常(checked)和不检查异常(unchecked),怎么下载内核源码可检查异常在源码里必须显示的进行捕获处理,这是编译期检查的一部分。不检查异常就是所谓的运行时异常,类似NullPointerException,ArrayIndexOutOfBoundsException之类,通常是可以编码避免的逻辑错误。
javaä¸ å¼å¸¸ç±»Exception(String message, Throwable cause)
æ¯å¼èµ·è¿ä¸ªå¼å¸¸çå¼å¸¸ï¼å¦æè¿ä¸ªå¼æ¯ç©ºå¼ï¼é£ä¹è¿ä¸ªå¼å¸¸å°±æ¯æºå¤´ï¼å¦æè¿ä¸ªå¼çäºèªå·±ï¼é£ä¹è¿ä¸ªå¼å¸¸è¿æ²¡è¢«åå§åã
ä¸é¢æ¯JDKä¸æºç ç»åºç注éï¼è±è¯å¥½çè¯ä¸çå°±æã
/
*** The throwable that caused this throwable to get thrown, or null if this
* throwable was not caused by another throwable, or if the causative
* throwable is unknown. If this field is equal to this throwable itself,
* it indicates that the cause of this throwable has not yet been
* initialized.
*
* @serial
* @since 1.4
*/
exceptionåerrorçåºå«
1.å¦å¾Exception å Erroré½ç»§æ¿èªThrowableç±»ï¼ç±jdk apiææ¡£ä¸å¯¹è¯¥ç±»çæè¿°å¯ä»¥ç¥éå®æ¯å¼å¸¸å¤çæºå¶çåºæ¬ç»æç±»åãå¯ä»¥ç±èææºæåºæè ç¼ç è èªè¡æåº(throw)jdk8ä¸æåç¿»è¯Throwableç±»çæè¿°ï¼Throwableç±»æ¯Javaè¯è¨ä¸ææé误åå¼å¸¸çThrowableç±»ã åªæä½ä¸ºæ¤ç±»ï¼æå ¶ä¸ä¸ªåç±»ï¼çå®ä¾ç对象ç±Javaèææºæåºï¼æè å¯ä»¥ç±Java throwè¯å¥æåºã 类似å°ï¼åªæè¿ä¸ªç±»æå ¶åç±»å¯ä»¥æ¯catchåå¥ä¸çåæ°ç±»åã
2.Exception å Error ä½ç°äº Java å¹³å°è®¾è®¡è 对ä¸åå¼å¸¸æ åµçåç±»ãException æ¯ç¨åºæ£å¸¸è¿è¡ä¸ï¼å¯ä»¥é¢æçæå¤æ åµï¼å¯è½å¹¶ä¸åºè¯¥è¢«æè·ï¼è¿è¡ç¸åºå¤çã
3.Error æ¯æå¨æ£å¸¸æ åµä¸ï¼ä¸å¤§å¯è½åºç°çæ åµï¼ç»å¤§é¨åç Error é½ä¼å¯¼è´ç¨åºï¼æ¯å¦ JVM èªèº«ï¼å¤äºéæ£å¸¸çãä¸å¯æ¢å¤ç¶æãæ¢ç¶æ¯éæ£å¸¸æ åµï¼æ以ä¸ä¾¿äºä¹ä¸éè¦æè·ï¼å¸¸è§çæ¯å¦ OutOfMemoryError ä¹ç±»ï¼é½æ¯ Error çåç±»ã
4.Exception åå为å¯æ£æ¥ï¼checkedï¼å¼å¸¸åä¸æ£æ¥ï¼uncheckedï¼å¼å¸¸ï¼å¯æ£æ¥å¼å¸¸å¨æºä»£ç éå¿ é¡»æ¾å¼å°è¿è¡æè·å¤çï¼è¿æ¯ç¼è¯ææ£æ¥çä¸é¨åãåé¢æä»ç»çä¸å¯æ¥ç Errorï¼æ¯ Throwable ä¸æ¯ Exceptionã
å¦ä½å¤çè¿äºå¼å¸¸ï¼
å¨ç°å®ç¼ç¨ä¸æ们ä¸è¬ä½¿ç¨ try-with-resources å multiple catchæ¥è¿è¡ä¸äºå¼å¸¸å¤ç(便å©çç¹æ§)ï¼å¨ç¼è¯æ¶æï¼ä¼èªå¨çæç¸åºçå¤çé»è¾ï¼æ¯å¦ï¼èªå¨æç §çº¦å®ä¿æ close é£äºæ©å±äº AutoCloseable æè Closeable ç对象ã
try-with-resources æ¯ä¸ç§å¤çCloseableå®ç°ç±»å ³éèµæºçä¸ç§åæ³ï¼ç®ååäºä¸ä¸ªä¾åå¯ä»¥åç°å ¶å®è¿å°±æ¯ä¸ä¸è¯æ³ï¼è¿ç§è¯æ³å¯ä»¥èªå¨ç¼è¯å¸®å¿ç¼è¯æåå ³éæµçæä½ï¼
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("a"));
BufferedWriter writer = new BufferedWriter(new FileWriter("a"))) {
// Try-with-resources
} catch (IOException e) { // Multiple catch
// Handle it
}
}
ç¼è¯åclass
public static void main(String[] args) { try {
BufferedReader br = new BufferedReader(new FileReader("a"));
Throwable var2 = null; try {
BufferedWriter writer = new BufferedWriter(new FileWriter("a"));
Object var4 = null; if (writer != null) { if (var4 != null) { try {
writer.close();
} catch (Throwable var) {
((Throwable)var4).addSuppressed(var);
}
} else {
writer.close();
}
}
} catch (Throwable var) {
var2 = var; throw var;
} finally { if (br != null) { if (var2 != null) { try {
br.close();
} catch (Throwable var) {
var2.addSuppressed(var);
}
} else {
br.close();
}
}
}
} catch (IOException var) {
}
}
å¼å¸¸å¤çç两个åºæ¬åå
å°½éä¸è¦æè·ç±»ä¼¼ Exception è¿æ ·çéç¨å¼å¸¸ï¼èæ¯åºè¯¥æè·ç¹å®å¼å¸¸
è¿æ¯å 为å¨æ¥å¸¸çå¼åååä½ä¸ï¼æ们读代ç çæºä¼å¾å¾è¶ è¿å代ç ï¼è½¯ä»¶å·¥ç¨æ¯é¨åä½çèºæ¯ï¼æ以æ们æä¹å¡è®©èªå·±ç代ç è½å¤ç´è§å°ä½ç°åºå°½éå¤çä¿¡æ¯ï¼èæ³æ³ç Exception ä¹ç±»ï¼æ°æ°éèäºæ们çç®çãå¦å¤ï¼æ们ä¹è¦ä¿è¯ç¨åºä¸ä¼æè·å°æ们ä¸å¸ææè·çå¼å¸¸ãæ¯å¦ï¼ä½ å¯è½æ´å¸æ RuntimeException 被æ©æ£åºæ¥ï¼èä¸æ¯è¢«æè·ã
ä¸è¦çåï¼swallowï¼å¼å¸¸ãè¿æ¯å¼å¸¸å¤çä¸è¦ç¹å«æ³¨æçäºæ ï¼å 为å¾å¯è½ä¼å¯¼è´é常é¾ä»¥è¯æç诡å¼æ åµã
å¦ææ们ä¸æå¼å¸¸æåºæ¥ï¼æè ä¹æ²¡æè¾åºå°æ¥å¿ï¼Loggerï¼ä¹ç±»ï¼ç¨åºå¯è½å¨åç»ä»£ç 以ä¸å¯æ§çæ¹å¼ç»æã没人è½å¤è½»æå¤æ究ç«æ¯åªéæåºäºå¼å¸¸ï¼ä»¥åæ¯ä»ä¹åå 产çäºå¼å¸¸ã
å¨åç¨åºæ¶å¯ä»¥éè¿Objectsç±»(jdkæä¾),æè æè¨çæåå¤æé®é¢ï¼å¦ç©ºæéå¼å¸¸çä¸äºå¼çå¤çObjects. requireNonNull(filename);èä¸æ¯ä½¿ç¨ä¹åæé®é¢åç³»ç»æåºå¼å¸¸ï¼è¿å¯è½å°±ä¼ä¸ç´è§çæ¾å°é®é¢æå¨ãå³:Throw early, catch late åå
å¸æ对æ¨ææ帮å©ï¼~