1.exceptionԴ?码查?鿴
2.javaä¸ å¼å¸¸ç±»Exception(String message, Throwable cause)
3.exceptionåerrorçåºå«
4.javaä¸å¸¸è§çå¼å¸¸ç±»
exceptionԴ??鿴
查看源码:链接如下:
androiddocs/src/trunk/core/java/android/content/BroadcastReceiver.java
public void sendFinished(IActivityManager am) {
synchronized (this) {
if (mFinished) {
throw new IllegalStateException("Broadcast already finished");
}
mFinished = true;
估计是重复调用finish方法去结束广播,第二次调用就会抛这个错,码查ssm车间网站源码你不能关闭两次。码查年溯源码
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 åå
å¸æ对æ¨ææ帮å©ï¼~
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å¼å¸¸å¤çæºå¶ä½¿ç¨ï¼æ¯å¼å¸¸å¤ççæ ¸å¿ã
åèèµæï¼ç¾åº¦ç¾ç§-å¼å¸¸
2024-12-23 00:42
2024-12-23 00:31
2024-12-23 00:28
2024-12-23 00:26
2024-12-23 00:12
2024-12-22 23:54
2024-12-22 23:49
2024-12-22 23:38