【码帮源码】【小源码健身】【千亿指标源码】throwable 源码
1.javaä¸å¸¸è§çå¼å¸¸ç±»
2.exception和error区别
3.throwableåExceptionçåºå«(详ç»ä¸ç¹)
4.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å¼å¸¸å¤çæºå¶ä½¿ç¨ï¼æ¯å¼å¸¸å¤ççæ ¸å¿ã
åèèµæï¼ç¾åº¦ç¾ç§-å¼å¸¸
exception和error区别
以java为例,其exception和error的区别如下:
1、Exception和Error都继承了Throwable类,在java中只有Throwable类型的码帮源码实例才可以被抛出(Throw)或者捕捉(catch),它是异常处理机制的基本组成类型。Exception是小源码健身程序正常运行中,可以预料的意外情况,可能并且应该被捕获,进行相应的处理。
2、Error指在正常情况下,不大可能出现的情况,绝大部门的千亿指标源码Error都会导致程序处于非正常的,不可恢复状态。既然是非正常情况,所以不便于也不需要捕获。周期套源码比如常见的OutOfMemoryError之类,都是Error的子类。
Exception类又分为可检查异常(checked)和不检查异常(unchecked),风车app源码可检查异常在源码里必须显示的进行捕获处理,这是编译期检查的一部分。不检查异常就是所谓的运行时异常,类似NullPointerException,ArrayIndexOutOfBoundsException之类,通常是可以编码避免的逻辑错误。
throwableåExceptionçåºå«(详ç»ä¸ç¹)
throwableåexceptionçåºå«ï¼1ãthrowableæ¯ç¶ç±»ï¼exceptionæ¯åç±»ã
2ãthrowableæ¯æ ¹åºï¼exceptionæ¯ä»throwableæ´¾çåºæ¥çã
3ãthrowableä¸å æ¬exceptionï¼å¼å¸¸ï¼åerrorï¼é误ï¼ã
4ãthrowableç¨æ¥å®ä¹ææå¯ä»¥ä½ä¸ºå¼å¸¸è¢«æåºæ¥çç±»ï¼exceptionä¸æç¨åºæ¬èº«å¯ä»¥å¤ççå¼å¸¸ï¼ä¸è¬æ§çå¼å¸¸ã
å¨Javaç¨åºä¸ï¼ææå¼å¸¸å¯¹è±¡çæ ¹åºç±»æ¯Throwableï¼Throwableä»Objectç´æ¥ç»§æ¿èæ¥ï¼è¿æ¯Javaç³»ç»æ强å¶è¦æ±çï¼ãThrowableæ两个éè¦çåç±»ï¼Exceptionï¼å¼å¸¸ï¼å Errorï¼é误ï¼ï¼äºè é½æ¯ Java å¼å¸¸å¤ççéè¦åç±»ï¼åèªé½å å«å¤§éåç±»ã
æ©å±èµæï¼Errorï¼é误ï¼æ¯ç¨åºæ æ³å¤ççé误ï¼è¡¨ç¤ºè¿è¡åºç¨ç¨åºä¸è¾ä¸¥éé®é¢ã大å¤æ°é误ä¸ä»£ç ç¼åè æ§è¡çæä½æ å ³ï¼è表示代ç è¿è¡æ¶ JVMï¼Java èææºï¼åºç°çé®é¢ãExceptionï¼å¼å¸¸ï¼æ¯ç¨åºæ¬èº«å¯ä»¥å¤ççå¼å¸¸ã
Erroræ¯ä¸ç§ä¸¥éçé®é¢ï¼åºç¨ç¨åºä¸åºè¯¥ææå®ã Exceptionä¸è¬å¯è½æ¯ç¨åºåä¸å¡ä¸çé误ï¼æ¯å¯ä»¥æ¢å¤çã
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 åå
å¸æ对æ¨ææ帮å©ï¼~