Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Chúng tôi cũng cho thấy làm thế nào để sửa đổi các định dạng đầu ra (ví dụ, làm thế nào để tăng số lượng các chữ số được in sau khi các điểm thập phân). Chúng tôi minh họa cho rất nhiều những ý tưởng này bằng cách tạo ra một lớp học để phân tích các tập tin | Odds and Ends 341 This program defines a procedure named quotient see lines 5-8 that takes two double arguments and returns their quotient. If this procedure is invoked with q equal to zero then the quotient is undefined. In this case the procedure throws an exception. Notice that there is no try catch block in the quotient procedure it is the responsibility of the procedure that invokes quotient in this example main to handle the exception. Inside main the call to quotient is embedded in a try block lines 12-21 . If quotient runs normally i.e. its second argument is not zero then line 18 executes normally. Execution then proceeds to line 20 then the catch block lines 22-25 is skipped and the program continues at line 27. However if at line 18 the second argument sent to quotient is zero then quotient throws an exception. The value of the numerator is thrown. The rest of the try block is skipped that is line 20 is not executed. At this point the computer searches for a catch statement whose type matches the type of the thrown value. Because a double value is thrown by quotient it is caught at line 22. Now the catch block executes with top set equal to the thrown value. An error message is printed line 23 followed by a call to a system procedure named exit. This causes the program to stop executing immediately. Consequently the statements following the catch block statements that would typically execute were it not for the call to exit are not executed. If possible the catch block should repair any damage caused by the exceptional situation so the program can continue running. However some errors are sufficiently serious that continued execution does not make sense. In that case a call to exit causes the program to stop running. Note that exit can be called inside any procedure not just in main . The exit procedure takes an integer valued argument this value is passed back to the operating system. If your C program is invoked by a script the script can use that .