Exceptions and exception handling are an important part in most applications. And modern programming languages support exceptions very well. Most of them provide try/catch/finally clauses for developers to handle exceptions.
Here we talk about how to write try/catch/finally clauses in a more elegant way.
Here, we take I/O operations as our illustrated example, because I/O conerns three important steps when using: initialization, exception handing and cleanup.
Generally, some programmer might write like this:
Such style has the following problems:
- There are extra try/catch inside outer try/catch to catch the same exception, which is might be redundant
- The variable input/output are declared beyond where they are needed, which have unnecessary life circle
- Two try/catch and try/catch in finally, which might be not elegant enough
The suggested way is like this:
This try/finally try/catch style is superior to previous version is because, it finishes all the tasks: declaration, initialization, operations and cleanup. All blocks are inside big try/catch so that all exceptions are get caught;
Cleanup is inside try/finally so that we ensure to release taken resources. It also looks elegant.
Such try/finally try/catch clauses, however, have a fatal defect that exceptions could be ignored and lost. In try/finally, finally block gets executed even if when exceptions are thrown in try block. If finally block also throws
exceptions, ones thrown in try blocks are ignored and lost. Outer catch clauses will never catch them. In all, this style might not catch all exceptions thrown in try blocks.
You can apply this try/finally try/catch style when:
- cleanup codes are not much and quite simple, like closing IO streams, shutdown connection.etc.
- cleanup codes are not likely to throw exceptions, or much less than initializations and operations.
- you do not care what exceptions are, but whether there are exceptions thrown
Besides these, you should use try/catch/finally, when specific exceptions are important in particular.
分享到:
相关推荐
This book is an outgrowth of a 1996 NIPS workshop called Tricks of the Trade whose goal was to begin the process of gathering and documenting these tricks. The interest that the workshop generated, ...
: CSS Triks: ://css-tricks.com 1stwebdesigner: ://1stwebdesigner.com webdesignerdepot: : 具体教程和初学者 PSD转换为HTML Webdesigntuts +从Photoshop到Wordpress: 链接* bagian lanjutan ...
Neural Networks: Tricks of the Trade, Second Edition Editors: Grégoire Montavon, Geneviève B. Orr, Klaus-Robert Müller 有关神经网络、深度学习Tricks的入门经典书籍。
3. **CSS Tricks** (https://css-tricks.com/):CSS Tricks 是一个充满实用技巧和深度文章的博客,涵盖了各种高级CSS技术,如Flexbox和Grid布局。 4. **CSS Zen Garden** (http://www.csszengarden.com/):通过展示...
Python技巧是Python社区中一种独特的交流方式,它通过简短而具有教学性的代码片段来传授Python语言的特性。这些技巧通常是在Twitter上分享的,它们可以是简单的示例,用以解释Python的某个特定概念,也可以是复杂的...
1 Introduction 2 Patterns for Cleaner Python 3 Effective Functions 4 Classes & OOP 5 Common Data Structures in Python ...7 Dictionary Tricks 8 Pythonic Productivity Techniques 9 Closing Thoughts
Drawing on years of experience teaching R courses, authors Colin Gillespie and Robin Lovelace provide practical advice on a range of topics—from optimizing the set-up of RStudio to leveraging C++—...
Android, the next-generation open mobile platform from Google and the Open Handset Alliance, is poised to become a significant player in the mobile device market. The Android platform gives developers...
《PyJSON_tricks:Python JSON处理的增强工具》 在Python的世界中,JSON(JavaScript Object Notation)是一种广泛使用的数据交换格式,它以其简洁、易读的特性深受开发者喜爱。然而,标准的`json`模块虽然能满足...
spring-boot-upload-file-lead-to-rce-tricks一. docker 漏洞环境搭建docker pull landgrey/spring-boot-fat-jar-write-file-rce:1.2docker run -d -p 18081:18081 landgrey/spring-boot-fat-jar-write-file-rce:1.2...
index.html initially creates a menu with four (4) menu items, each with a name and Datafeeder (URL) from which the data loads into the page after executing Jasmine Test scripts successfully. ...
The Fastest and Easiest Way for Beginners to Develop Android Apps What’s Stopping You From Making Your Own Android Apps? The mobile phone app market is worth more than $25 billion and is expected ...
Use the search field in the nav bar to search all of the CommonsWare books to see what they hold! Key Android Concepts Choosing Your Development Toolchain Tutorial #1 - Installing the Tools Android ...
Note: the latest edition of this book is jQuery: Novice to Ninja: New Kicks And Tricks jQuery: Novice to Ninja is your fast track to mastering jQuery—the all-conquering JavaScript framework. Used by...
Genevieve B. Orr Klaus-Robert Muller (Eds.) Neural Networks: Tricks of the Trade 本书分为17篇小文章,每篇都会介绍神经网络的技巧。(425页)
Tricks of The 3D Game Programming Gurus Code
现场演示请访问 https://shoppity.azurewebsites.net/解释该演示的文章将很快在https://css-tricks.com/上发布无服务器功能也是开源的,可从此 repo https://github.com/sdras/sample-stripe-handler获取构建设置# ...
Neural Networks_ Tricks of the Trade_ Second Edition