`
leonzhx
  • 浏览: 793368 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 4. Error Handling

阅读更多

1.  Trying to read a property from an undefined value, or using a function that does not exists, will cause an error to be signaled.

 

2.  Checking input is always a judgment call—you have to identify the mistakes that are likely to occur and that are likely to have subtle, complicated effects (rather than just causing an error right away).

 

3.  When a function encounters a problem that it cannot solve itself, one possible reaction is to return a value that it could not normally return. It does, however, have its downsides. First, what if the function can already return every possible kind of value? The second issue with returning special values is that it can sometimes lead to a whole lot of clutter. If a piece of code is called 10 times, you have to check 10 times whether the special value was returned.

 

4.  It is possible for code to raise (or throw) an exception, which is a value. Raising an exception somewhat resembles a super-charged return from a function—it does not just jump out of the current function but also out of its callers, all the way up to the top-level call that started the current execution. This is called unwinding the stack. It is possible to set obstacles for exceptions along the stack. These catch the exception as it is zooming down and can do something with it, after which the program continues running at the point where the exception was caught.

 

5.  throw is the keyword that is used to raise an exception. The keyword try sets up an obstacle for exceptions: When the code in the block after it raises an exception, the catch block will be executed. The variable named in parentheses after the word catch will hold the exception value when this block executes. try statements can also be followed by a finally keyword, which means “no matter what happens, run this code after trying to run the code in the try block.” If a function has to clean something up, the cleanup code should usually be put into a finally block.

 

6.  A special type of objects is raised for problems like getting properties from undefined. These always have a message property containing a description of the problem. You can raise similar objects using the new keyword and the Error constructor, giving the message as argument: throw new Error("Wolf!");

分享到:
评论

相关推荐

    iOS.9.App.Development.Essentials

    Chapter 4. A Guided Tour of Xcode 7 Chapter 5. An Introduction to Xcode 7 Playgrounds Chapter 6. Swift Data Types, Constants and Variables Chapter 7. Swift Operators and Expressions Chapter 8. Swift ...

    Scientific.Computing.with.Python.3.2nd.Ed.epub

    Error Handling Chapter 11. Namespaces, Scopes, and Modules Chapter 12. Input and Output Chapter 13. Testing Chapter 14. Comprehensive Examples Chapter 15. Symbolic Computations - SymPy

    Ajax for Web Application Developers(Ajax网站开发)

    Chapter 4. Rendering the Response with XHTML and CSS XHTML CSS Part II: Creating and Using the JavaScript Engine Chapter 5. Object-Oriented JavaScript Object-Oriented Approaches Using the...

    MySql存储过程编程.chm

    Chapter 4. Blocks, Conditional Statements, and Iterative Programming Section 4.1. Block Structure of Stored Programs Section 4.2. Conditional Control Section 4.3. Iterative Processing with ...

    Quality Code: Software Testing Principles, Practices, and Patterns

    Chapter 4. Design and Testability Chapter 5. Testing Principles Part II: Testing and Testability Patterns Chapter 6. The Basics Chapter 7. String Handling Chapter 8. Encapsulation and Override ...

    Swift.2.Cookbook.17858892

    With Swift 2 comes even better performance, a new error handling API, protocol extensions, and super support for availability checking. This book will equip you with all the practical programming ...

    Windows Internals-Fourth Edition.chm

    Chapter 4. Management Mechanisms The Registry Services Windows Management Instrumentation Conclusion Chapter 5. Startup and Shutdown Boot Process Troubleshooting Boot and Startup ...

    Programming Microsoft ASP.NET MVC, 3rd Edition

    Design websites for mobile devices, localization, and error handling Provide security by implementing a membership system Inject script code into your site using JavaScript and jQuery Use Responsive ...

    Rust.Essentials.2nd.Edition.epub

    Chapter 4. Structuring Data And Matching Patterns Chapter 5. Higher Order Functions And Error-Handling Chapter 6. Using Traits And Oop In Rust Chapter 7. Ensuring Memory Safety And Pointers Chapter 8....

    The way to go

    1.2.1 Languages that influenced Go.........................................................................4 1.2.2 Why a new language?....................................................................

    UNIX Network Programming Volume 1, Third Edition (Unix网络编程卷1第3版英文版)

    Chapter 4. Elementary TCP Sockets Section 4.1. Introduction Section 4.2. socket Function Section 4.3. connect Function Section 4.4. bind Function Section 4.5. listen Function Section 4.6....

    Learning.Reactive.Programming.With.Java.8

    Combinators, Conditionals, and Error Handling Chapter 6. Using Concurrency and Parallelism with Schedulers Chapter 7. Testing Your RxJava Application Chapter 8. Resource Management and Extending ...

    Developing Flex Applications 910p dda_doc88_cracker.zip

    CHAPTER 4: Developing Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 About the Flex coding process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

    [Go语言入门(含源码)] The Way to Go (with source code)

    1.2.1 Languages that influenced Go.........................................................................4 1.2.2 Why a new language?....................................................................

    Yii.Application.Development.Cookbook.3rd.Edition

    Key Features Learn how to use Yii2 efficiently through clear examples and core features, and see how to use tests, create reusable code snippets, core widgets, ... Debugging, Logging, and Error Handling

Global site tag (gtag.js) - Google Analytics