SlideShare a Scribd company logo
1 of 10
W31 Error Handling:
Error Code vs. Exception
Chieh Yu @ 2021/08/03
Outline
● Error Code
● Exception
● 效能影響
● 語言慣例
● 結論
Error Code
● 使用 return value 或 global variable 來處理錯誤
○ if ((rc = foo()) < 0) { // … } / errno & GetLastError()
● 優點
○ 簡單好懂
○ 使用上簡單
○ 錯誤處理貼近發生錯誤的位置
○ 和 C library 整合容易
● 缺點
○ 重複的 code 多 => 重複出現時寫起來重複、看起來也雜訊多
○ 錯誤處理和正常流程耦合度高
○ 若沒有處理,程式可能運作在好與不好之間
Exceptions
● 使用 try-catch 來處理錯誤
● 優點
○ 強迫開發一定要處理錯誤,否則程式會結束
○ 可以集中錯誤偵測與處理的 code
○ 正常流程的 code 可以更緊湊
○ 錯誤處理跨越 function call,中間的 layer 可以不用管其他層
● 缺點
○ 概念較複雜,需妥善設計錯誤處理位置
○ 若沒有好好處理,錯誤可能消失於無形之中
○ 古早時期認為效能較差 (已經不存在)
效能影響
● Error code
○ 需檢查 return value
○ 若 return value 傳遞是靠 memory 影響更大
● Exception
○ try block 需要 data structure 追蹤 stack,佔用一些記憶體
○ 需要一些 code 去檢查是否有噴出 exception 和 unwind stack
○ 若沒發生 throw, 效能影響微乎其微
● 測試結果 [1][2]
○ 看情況,寫的好,Exception 比較快
○ 在 performance critical 的 loop 內,正常的流程與錯誤處理高度耦合且固定會發生錯誤的情況 => Error
code
https://dr-knz.net/go-errors-vs-exceptions-2020.html
語言慣例
● C: error code
● C++: error code and exception (M$ 說主流是 exception)
● Java/Kotlin: exception
● Go: error code
● Rust: error code
● JavaScript: 無 (誤)
● Note: 文章都不錯看,可以參考一下
結論
● Error code 和 Exception 都好棒棒
○ 各自有適合的場景
● 其實混用並不衝突
○ 如在 performance critical 的 loop 內,正常的流程與錯誤處理高度耦合且固定會發生錯誤的情況
。e.g. HTTP server no such URL
● 建議依照語言慣例使用
○ 若沒有慣例則由團隊 / 公司建立共識
Thank you~ 😇

More Related Content

Similar to Error handling: error code vs. exception

Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
olracoatalub
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
kimberly_Bm10203
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
gerrell
 

Similar to Error handling: error code vs. exception (20)

An introduction to programming in Go
An introduction to programming in GoAn introduction to programming in Go
An introduction to programming in Go
 
Go lang
Go langGo lang
Go lang
 
go language- haseeb.pptx
go language- haseeb.pptxgo language- haseeb.pptx
go language- haseeb.pptx
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
 
Python exceptions
Python exceptionsPython exceptions
Python exceptions
 
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerException Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim Muller
 
Debugging Drupal with Xdebug
Debugging Drupal with XdebugDebugging Drupal with Xdebug
Debugging Drupal with Xdebug
 
Survelaine murillo ppt
Survelaine murillo pptSurvelaine murillo ppt
Survelaine murillo ppt
 
High Performance Haskell
High Performance HaskellHigh Performance Haskell
High Performance Haskell
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
 
AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8
 
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
 
Try the monad!
Try the monad!Try the monad!
Try the monad!
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
 
My final requirement
My final requirementMy final requirement
My final requirement
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
Go fundamentals
Go fundamentalsGo fundamentals
Go fundamentals
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Error handling: error code vs. exception

  • 1. W31 Error Handling: Error Code vs. Exception Chieh Yu @ 2021/08/03
  • 2. Outline ● Error Code ● Exception ● 效能影響 ● 語言慣例 ● 結論
  • 3. Error Code ● 使用 return value 或 global variable 來處理錯誤 ○ if ((rc = foo()) < 0) { // … } / errno & GetLastError() ● 優點 ○ 簡單好懂 ○ 使用上簡單 ○ 錯誤處理貼近發生錯誤的位置 ○ 和 C library 整合容易 ● 缺點 ○ 重複的 code 多 => 重複出現時寫起來重複、看起來也雜訊多 ○ 錯誤處理和正常流程耦合度高 ○ 若沒有處理,程式可能運作在好與不好之間
  • 4. Exceptions ● 使用 try-catch 來處理錯誤 ● 優點 ○ 強迫開發一定要處理錯誤,否則程式會結束 ○ 可以集中錯誤偵測與處理的 code ○ 正常流程的 code 可以更緊湊 ○ 錯誤處理跨越 function call,中間的 layer 可以不用管其他層 ● 缺點 ○ 概念較複雜,需妥善設計錯誤處理位置 ○ 若沒有好好處理,錯誤可能消失於無形之中 ○ 古早時期認為效能較差 (已經不存在)
  • 5.
  • 6. 效能影響 ● Error code ○ 需檢查 return value ○ 若 return value 傳遞是靠 memory 影響更大 ● Exception ○ try block 需要 data structure 追蹤 stack,佔用一些記憶體 ○ 需要一些 code 去檢查是否有噴出 exception 和 unwind stack ○ 若沒發生 throw, 效能影響微乎其微 ● 測試結果 [1][2] ○ 看情況,寫的好,Exception 比較快 ○ 在 performance critical 的 loop 內,正常的流程與錯誤處理高度耦合且固定會發生錯誤的情況 => Error code
  • 8. 語言慣例 ● C: error code ● C++: error code and exception (M$ 說主流是 exception) ● Java/Kotlin: exception ● Go: error code ● Rust: error code ● JavaScript: 無 (誤) ● Note: 文章都不錯看,可以參考一下
  • 9. 結論 ● Error code 和 Exception 都好棒棒 ○ 各自有適合的場景 ● 其實混用並不衝突 ○ 如在 performance critical 的 loop 內,正常的流程與錯誤處理高度耦合且固定會發生錯誤的情況 。e.g. HTTP server no such URL ● 建議依照語言慣例使用 ○ 若沒有慣例則由團隊 / 公司建立共識

Editor's Notes

  1. https://dr-knz.net/go-errors-vs-exceptions-2020.html