Chương 3 LUỒNG DỮ LIỆU
Số trang: 133
Loại file: ppt
Dung lượng: 785.00 KB
Lượt xem: 31
Lượt tải: 0
Xem trước 10 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Trình biên dịch không yêu cầu phải bắt các biệt lệ khi nó xảy ra.Không cần khối try-catchCác biệt lệ này có thể xảy ra bất cứ thời điểm nào khi thi hành chương trình.Thông thường là những lỗi nghiêm trọng mà chương trình không thể kiểm soátXử dụng các mệnh đề điều kiện để xử lý sẽ tốt hơn.Ví dụ: NullPointerException,IndexOutOfBoundsException, ArithmeticException…
Nội dung trích xuất từ tài liệu:
Chương 3 LUỒNG DỮ LIỆU Chương 3LUỒNG DỮ LIỆU Nội dung• Xử lý biệt lệ• Luồng dữ liệu• Thao tác trên tập tin Exception HandlingXử lý mỗi sử dụng cơ chế biệt lệ trong Java Các cách xử lý lỗi• Sử dụng các mệnh đề điều kiện kết hợp với các giá trị cờ.• Sử dụng cơ chế xử lý biệt lệ. Ví dụ: Lớp Inventorypublic class Inventory{ public final int MIN = 0; public final int MAX = 100; public final int CRITICAL = 10; public boolean addToInventory (int amount) { int temp; temp = stockLevel + amount; if (temp > MAX) { System.out.print(Adding + amount + item will cause stock ); System.out.println(to become greater than + MAX + units (overstock)); return false; } Ví dụ: Lớp Inventory (2) else { stockLevel = stockLevel + amount; return true; }} // End of method addToInventory: Các vấn đề đối với cách tiếp cận điều kiện/cờreference1.method1 () if (reference2.method2() == false) return false; reference2.method2 () if (store.addToInventory(amt) == false) return false; store.addToInventory (int amt) if (temp > MAX) return false; Các vấn đề đối với cách tiếp cận điều kiện/cờreference1.method1 () Vấn đề 1: Phương thức if (reference2.method2() == false) chủ có thể quên kiểm tra return false; điều kiện trả về reference2.method2 () if (store.addToInventory(amt) == false) return false; store.addToInventory (int amt) if (temp > MAX) return false; Các vấn đề đối với cách tiếp cận điều kiện/cờ reference1.method1 () if (reference2.method2() == false) return false; reference2.method2 () if (store.addToInventory(amt) == false) return false; store.addToInventory (int amt)Vấn đề 2: Phải sử if (temp > MAX)dụng 1 loạt các phép return false;kiểm tra giá trị cờ trảvề Các vấn đề đối với cách tiếp cận điều kiện/cờ reference1.method1 () if (reference2.method2() == false) return false; reference.method2 () if (store.addToInventory(amt) == false) return false; ?? ?? store.addToInventory (int amt)Vấn đề 3: Phương thức if (temp > MAX)chủ có thể không biết return false;cách xử lý khi lỗi xảy ra Các cách xử lý lỗi• Sử dụng các mệnh đề điều kiện kết hợp với các giá trị cờ.• Sử dụng cơ chế xử lý biệt lệ. Xử lý biệt lệ• Cú pháp: try { // Code that may cause an error/exception to occur } catch (ExceptionType identifier) { // Code to handle the exception } Xử lý biệt lệ: đọc dữ liệu từ bàn phímimport java.io.*;class Driver{ public static void main (String [] args) { BufferedReader stringInput; InputStreamReader characterInput; String s; int num; characterInput = new InputStreamReader(System.in); stringInput = new BufferedReader(characterInput); Xử lý biệt lệ: đọc dữ liệu từ bàn phím try { System.out.print(Type an integer: ); s = stringInput.readLine(); System.out.println(You typed in... + s); num = Integer.parseInt (s); System.out.println(Converted to an integer... + num); } catch (IOException e) { System.out.println(e); } catch (NumberFormatException e) { : : : } }} Xử lý biệt lệ: Biệt lệ xảy ra khi nàotry { System.out.print(Type an integer: ); s = stringInput.readLine(); System.out.println(You typed in... + s); num = Integer.parseInt (s); System.out.println(Converted to an inte ...
Nội dung trích xuất từ tài liệu:
Chương 3 LUỒNG DỮ LIỆU Chương 3LUỒNG DỮ LIỆU Nội dung• Xử lý biệt lệ• Luồng dữ liệu• Thao tác trên tập tin Exception HandlingXử lý mỗi sử dụng cơ chế biệt lệ trong Java Các cách xử lý lỗi• Sử dụng các mệnh đề điều kiện kết hợp với các giá trị cờ.• Sử dụng cơ chế xử lý biệt lệ. Ví dụ: Lớp Inventorypublic class Inventory{ public final int MIN = 0; public final int MAX = 100; public final int CRITICAL = 10; public boolean addToInventory (int amount) { int temp; temp = stockLevel + amount; if (temp > MAX) { System.out.print(Adding + amount + item will cause stock ); System.out.println(to become greater than + MAX + units (overstock)); return false; } Ví dụ: Lớp Inventory (2) else { stockLevel = stockLevel + amount; return true; }} // End of method addToInventory: Các vấn đề đối với cách tiếp cận điều kiện/cờreference1.method1 () if (reference2.method2() == false) return false; reference2.method2 () if (store.addToInventory(amt) == false) return false; store.addToInventory (int amt) if (temp > MAX) return false; Các vấn đề đối với cách tiếp cận điều kiện/cờreference1.method1 () Vấn đề 1: Phương thức if (reference2.method2() == false) chủ có thể quên kiểm tra return false; điều kiện trả về reference2.method2 () if (store.addToInventory(amt) == false) return false; store.addToInventory (int amt) if (temp > MAX) return false; Các vấn đề đối với cách tiếp cận điều kiện/cờ reference1.method1 () if (reference2.method2() == false) return false; reference2.method2 () if (store.addToInventory(amt) == false) return false; store.addToInventory (int amt)Vấn đề 2: Phải sử if (temp > MAX)dụng 1 loạt các phép return false;kiểm tra giá trị cờ trảvề Các vấn đề đối với cách tiếp cận điều kiện/cờ reference1.method1 () if (reference2.method2() == false) return false; reference.method2 () if (store.addToInventory(amt) == false) return false; ?? ?? store.addToInventory (int amt)Vấn đề 3: Phương thức if (temp > MAX)chủ có thể không biết return false;cách xử lý khi lỗi xảy ra Các cách xử lý lỗi• Sử dụng các mệnh đề điều kiện kết hợp với các giá trị cờ.• Sử dụng cơ chế xử lý biệt lệ. Xử lý biệt lệ• Cú pháp: try { // Code that may cause an error/exception to occur } catch (ExceptionType identifier) { // Code to handle the exception } Xử lý biệt lệ: đọc dữ liệu từ bàn phímimport java.io.*;class Driver{ public static void main (String [] args) { BufferedReader stringInput; InputStreamReader characterInput; String s; int num; characterInput = new InputStreamReader(System.in); stringInput = new BufferedReader(characterInput); Xử lý biệt lệ: đọc dữ liệu từ bàn phím try { System.out.print(Type an integer: ); s = stringInput.readLine(); System.out.println(You typed in... + s); num = Integer.parseInt (s); System.out.println(Converted to an integer... + num); } catch (IOException e) { System.out.println(e); } catch (NumberFormatException e) { : : : } }} Xử lý biệt lệ: Biệt lệ xảy ra khi nàotry { System.out.print(Type an integer: ); s = stringInput.readLine(); System.out.println(You typed in... + s); num = Integer.parseInt (s); System.out.println(Converted to an inte ...
Tìm kiếm theo từ khóa liên quan:
xử lý tương tác hệ thống thông tin lập trình dữ liệu hệ thống dữ liệu xử lý thông tin Xử lý biệt lệ Luồng dữ liệuGợi ý tài liệu liên quan:
-
Bài tập thực hành môn Phân tích thiết kế hệ thống thông tin
6 trang 321 0 0 -
Đáp án đề thi học kỳ 2 môn cơ sở dữ liệu
3 trang 313 1 0 -
PHÂN TÍCH THIẾT KẾ HỆ THỐNG XÂY DỰNG HỆ THỐNG ĐẶT VÉ TÀU ONLINE
43 trang 281 2 0 -
Bài thuyết trình Hệ thống thông tin trong bệnh viện
44 trang 251 0 0 -
Bài giảng HỆ THỐNG THÔNG TIN KẾ TOÁN - Chương 2
31 trang 234 0 0 -
Tóm tắt luận án tiến sỹ Một số vấn đề tối ưu hóa và nâng cao hiệu quả trong xử lý thông tin hình ảnh
28 trang 222 0 0 -
Phương pháp và và ứng dụng Phân tích thiết kế hệ thống thông tin: Phần 1 - TS. Nguyễn Hồng Phương
124 trang 217 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng quản lý kho hàng trên nền Web
61 trang 215 0 0 -
62 trang 209 2 0
-
Tài liệu học tập môn Tin cơ sở: Phần 1 - Phùng Thị Thu Hiền
100 trang 190 1 0