Professional Documents
Culture Documents
• Ngoại lệ thứ 1: do người lập trình bắt lỗi với các tình
huống riêng như: chia 0, vượt qua chỉ số mảng,…
• Ngoại lệ thứ 2: do máy bắt lỗi đưa ra: ví dụ cấp phát
động, mở file… thì ta sử dụng trực tiếp lớp ngoại lệ
<exception>
Xử lý ngoại lệ - try ….catch
try{
// try block
}
catch( type1 arg){
// catch block
}
.
.
catch(typeN arg){
// catch block
}
Xử lý ngoại lệ thường sử dụng để kết nối những lỗi rất nặng (chia 0)
+ Khi xử lý được đưa ra, điều
Ví dụ - try_catch_3.cpp khiển được chuyển cho catch, và
khối try bị dừng.
+ Chương trình tiếp tục dòng
lệnh trong catch
int main(){ + Tuy nhiên khối catch thường
cout<<"start \n"; kết thúc bằng cách gọi exit(),
try{// bat dau khoi lenh abort().
cout<<"ben trong khoi lenh try \n";
throw 99;// dua ra mot loi
cout<<"Doan nay khong duoc goi \n";
}
catch(int i){ // bat loi
cout<<"Bat mot ngoai len -- gia tri i = ";
cout<<i<<"\n";
}
cout<<"end \n";
}
Sử dụng nhiều câu lệnh catch – try_catch_4.cpp
• Có thể kết hợp một câu lệnh try với nhiều câu lệnh catch()
try {
if(test==0) throw "value is zero";
if(test==1) throw 10;
if(test==2) throw 'a';
if(test==3) throw 123.23;
}
catch(int i){
cout << "Caught an integer. " << i << endl;
}
catch(double i){
cout << "Caught an double. " << i << endl;
}
catch (char c) {
cout << "Caught an char " << c << endl;
}
catch(const char *str) {
cout<<"bat mot xau ky tu (test = 0): str = ";
cout<<str<<endl;
}
catch(...){ // bat toan bo loi neu tren thieu catch cho loai nao thi tat ca chu vao day
cout << "Caught one." << endl;
}
Rethrowing an exception – try_catch_5.cpp
sử dụng một cảnh báo cho nhiều nơi
void XHandler(){
try{
throw "hello";
}
catch(const char *) {
cout<<"bat throw trong ham \n";
throw; // nem tro lai char * cho ben ngoai ham
}
}
int main()
{
try{
XHandler();
}
catch(const char * ){
cout<<"bat throw ngoai ham - trong ham main()\n“;
}
}
Ví dụ về ma trận (ngoại lệ 1)
try {
nhapmat(a, row_a, col_a);
nhapmat(b, row_b, col_b);
if (col_a!= row_b) {
throw 1;
}
tichMatran_AB(a,b,c,row_a, col_a);
disp(c,row_a,col_b);
}
catch(int s)
{
cout<<"Khong the thuc hien phep nhan tich 2 ma tran \n";
exit(1);
}
Ngoại lệ 2: exception - #include <exception>
• Tất cả các ngoại lệ được ném (throw) bởi các thành phần của
thư viện chuẩn C ++ exception, dẫn xuất từ lớp exception.
exception description
bad_alloc thrown by new on allocation failure
bad_cast thrown by dynamic_cast when it fails in a dynamic cast
bad_exception thrown by certain dynamic exception specifiers
bad_typeid thrown by typeid
bad_function_call thrown by empty function objects
bad_weak_ptr thrown by shared_ptr when passed a bad weak_ptr
int main () {
try{
int* myarray= new int[1000]; // bad_alloc
}
catch (exception& e){
cout << "Standard exception: " << e.what() << endl;
}
return 0;
}
Ví dụ: try – catch8.cpp
• Manipulator Declaration in
endl iostream.h
setw iomanip.h
setprecision iomanip.h
setf iomanip.h
Cờ định dạng
• Output function:
• ostream &manip_name(ostream &stream) {
// code here
return stream;
}
• input manipulator function
• istream &manip_name(istream &stream)
{
// code here
return stream;
}
Biến static – từ khóa static
• Biến kiểu static là một biến vĩnh cửu trong hàm riêng hoặc trong
file.
• Nó khác biến toàn cục bởi vì nó bên ngoài hàm hoặc file / 203.
• static: tác động lên các biến cục bộ khác
• Biến static cục bộ
• Khai báo : static int count; // giống như const int count = 10;
• static int count = 10; // khởi đầu
• Một biến cục bộ static duy trì giá trị của nó giữa các lần gọi hàm
• Ví dụ: static cục bộ
• static toàn cục
Trao đổi – vấn đề
• Từ khóa extern: sử dụng khi một chương trình viết trên nhiều
tệp và các tệp này được dịch độc lập sau đó mới liên kết với
nhau để tạo thành chương trình khả thi (.exe)
• Ví dụ: extern int b_chung;
• Khai báo extern báo cho chương trình dịch biết biến b_chung
đã được định nghĩa ở đâu đó. Do đó, không cần cấp phát bộ
nhớ cho nó một lần nữa
• Ví dụ 1: trong trường hợp này trong file: addToprojet9_1.cpp
định nghĩa hàm tích của 2 biến x*y
extern int x, y; // gia trị x, y thay đổi dựa vào hàm main()
int func(){
return x*y;
}
Vị trí khai báo và phạm vi sử dụng của biến extern
• Ví dụ 2:
• Vị trí khai báo extern:
Có thể ngoài các hàm
Có thể bên trong một hàm hoặc một khối lệnh.
• Phạm vi sử dụng của biến extern (nguyên lý): Dù khai báo ở
đâu thì phạm vi sử dụng của biến extern (trên tệp chứa khai
báo này) được tính từ vị trí khai báo cho đến cuối tệp.
• Do đó: Mảng a có thể sử dụng trên toàn tệp
Biến x khai bao trong disp_xExtern(): chỉ sử dụng trong
hàm
• TH1: Kết nối biến tổng thể không là hằng: (cpp)
//fileA.cpp //fileB.cpp (file sử dụng)
// declaration and definition // declaration only same as i in FileA
int i = 42; extern int i;
• Xem ví dụ
• Hướng dẫn tạo file.
• Cách sử dụng
• Cách gọi hàm.
• #include<windows.h>
• char program[] = "Notepad.exe MATRAN.txt";
• WinExec(program, SW_SHOWNORMAL);
char program[] = "C:\\Program Files\\Microsoft
Office\\Office16\\EXCEL.EXE a2.csv";
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-winexec
Value Meaning
SW_HIDE Hides the window and activates another window.
0
SW_SHOWNORMAL Activates and displays a window. If the window is minimized or
SW_NORMAL maximized, the system restores it to its original size and position.
1 An application should specify this flag when displaying the window
for the first time.
SW_SHOWMINIMIZED Activates the window and displays it as a minimized window.
2
SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window.
SW_MAXIMIZE
3
SW_SHOWNOACTIVATE Displays a window in its most recent size and position. This value is
4 similar to SW_SHOWNORMAL, except that the window is not
activated.
SW_SHOW Activates the window and displays it in its current size and position.
5
SW_MINIMIZE Minimizes the specified window and activates the next top-level
6 window in the Z order.
SW_SHOWMINNOACTIVE Displays the window as a minimized window. This value is similar
7 to SW_SHOWMINIMIZED, except the window is not activated.
SW_SHOWNA Displays the window in its current size and position. This value is
8 similar to SW_SHOW, except that the window is not activated.
SW_RESTORE Activates and displays the window. If the window is minimized or
9 maximized, the system restores it to its original size and position.
An application should specify this flag when restoring a minimized
window.
SW_SHOWDEFAULT Sets the show state based on the SW_ value specified in
10 the STARTUPINFO structure passed to the CreateProcess function
by the program that started the application.
SW_FORCEMINIMIZE Minimizes a window, even if the thread that owns the window is
11 not responding. This flag should only be used when minimizing
windows from a different thread.
Ứng dụng sử dụng lệnh: system
• https://www.tutorialspoint.com/c_standard_library/c_function_clock.htm
• Tính thời gian của một đoạn chương trình (cả thời
gian chờ) #include<time.h>
• start_t = clock();
-----
-----
-----
• end_t = clock();
• total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
(second)