`
leonzhx
  • 浏览: 771792 次
  • 性别: 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!");

分享到:
评论

相关推荐

    Mastering.Apache.Camel

    An advanced guide to Enterprise Integration using Apache ...Chapter 4. Beans Chapter 5. Enterprise Integration Patterns Chapter 6. Components and Endpoints Chapter 7. Error Handling Chapter 8. Testing

    Spring.REST.1484208242

    Chapter 4. Beginning the QuickPoll Application Chapter 5. Error Handling Chapter 6. Documenting REST Services Chapter 7. Versioning, Paging, and Sorting Chapter 8. Security Chapter 9. Clients and ...

    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 ...

    Learning.JavaScript.Add.Sparkle.and.Life.to.Your.Web.Pages.3rd.Edition.14

    Exceptions and Error Handling Chapter 12. Iterators and Generators Chapter 13. Functions, and the Power of Abstract Thinking Chapter 14. Asynchronous Programming Chapter 15. Date and Time Chapter 16....

    ILE/RPG(Reference).pdf

    Error Handling Chapter 2. RPG Character Set Chapter 3. Control Specifications Chapter 4. File Description Specifications Chapter 5. Extension Specifications Chapter 6. Line Counter Specifications ...

    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 ...

    iron-clad.java.building.secure.web.applications

    Chapter 4. Cross-Site Scripting Defense Chapter 5. Cross-Site Request Forgery Defense and Clickjacking Chapter 6. Protecting Sensitive Data Chapter 7. SQL Injection and Other Injection Attacks Chapter...

    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 ...

    OpenGL Shading Language, Second Edition

    Error Handling Section 3.11. Summary Section 3.12. Further Information Chapter 4. The OpenGL Programmable Pipeline Section 4.1. The Vertex Processor Section 4.2. The Fragment Processor Section 4.3. ...

    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....

    Infinity Science – Software Engineering and Testing.pdf

    Chapter 4. Software Reliability and Quality Assurance 85 4.1 Verification and Validation 85 4.2 Software Quality Assurance 87 4.3 Software Quality 89 4.4 Capability Maturity Model (SEI-CMM...

    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 ...

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

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

Global site tag (gtag.js) - Google Analytics