TAILIEUCHUNG - Dive Into Python-Chapter 6. Exceptions and File Handling

Dive vào Python là một cuốn sách cho các lập trình Python có kinh nghiệm. Cuốn sách này được cung cấp ở định dạng khác nhau, bao gồm HTML, PDF, đồng bằng văn bản, Microsoft Word và XML. Ngoài ra, Dive vào Python đã được dịch sang nhiều ngôn ngữ, do đó bạn có thể chọn một trong những phù hợp với nhu cầu của bạn | Chapter 6. Exceptions and File Handling In this chapter you will dive into exceptions file objects for loops and the os and sys modules. If you ve used exceptions in another programming language you can skim the first section to get a sense of Python s syntax. Be sure to tune in again for file handling. . Handling Exceptions Like many other programming languages Python has exception handling via blocks. Note Python uses to handle exceptions and raise to generate them. Java and C use to handle exceptions and throw to generate them. Exceptions are everywhere in Python. Virtually every module in the standard Python library uses them and Python itself will raise them in a lot of different circumstances. You ve already seen them repeatedly throughout this book. Accessing a non-existent dictionary key will raise a KeyError exception. Searching a list for a non-existent value will raise a ValueError exception. Calling a non-existent method will raise an AttributeError exception. Referencing a non-existent variable will raise a NameError exception. Mixing datatypes without coercion will raise a TypeError exception. In each of these cases you were simply playing around in the Python IDE an error occurred the exception was printed depending on your IDE perhaps in an intentionally jarring shade of red and that was that. This is called an unhandled exception. When the exception was raised there was no code to explicitly notice it and deal with it so it bubbled its way back to the default behavior built in to Python which is to spit out some debugging information and give up. In the IDE that s no big deal but if that happened while your actual Python program was running the entire program would come to a screeching halt. An exception doesn t need result in a complete program crash though. Exceptions when raised can be handled. Sometimes an exception is really because you have a bug in your code like accessing a variable that doesn t exist but

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.