By GokiSoft.com|
15:05 26/06/2023|
Java Basic
[Share Code] Tìm hiểu về Exception & Try Catch Trong Java - C2209I
Nội dung kiến thức:
- Exception
-> Exception là gì?
-> try .. catch .. finally
-> Custom Exception
- Lambda
-> Áp dụng với interface có 1 function
- Một số hàm tiện ích trong Java
20 ung dung -> cung chay
game -> P
- UI -> T1
- download file -> T2
- music (sound) ...
- animation ...
....
-> Chay // -> ???
-> Tuan tu
20 ung dung -> p1 -> p20
CPU: 1 trieu/1s
Da tien trinh: OS
#AgeException.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson07;
/**
*
* @author teacher
*/
public class AgeException extends Exception{
}
#Animal.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson07;
/**
*
* @author teacher
*/
public abstract class Animal {
String name;
public Animal() {
}
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void showSound();
}
#IRunning.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson07;
/**
*
* @author teacher
*/
public interface IRunning {
void onRunning();
}
#IShow.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package com.gokisoft.c2209i.lesson07;
/**
* @author teacher
*/
@FunctionalInterface
public interface IShow {
void showMessage(String msg);
}
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson07;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
//Crash -> Exception: Logic (Viết code lỗi), third party (Thư viên sử dụng), System (Thiết bị phần cứng, network, file, ...)
//Logic
/**
Scanner scan = new Scanner(System.in);
System.out.println("Nhap x = ");
int x = scan.nextInt(); //Nhap ky tu ko phai so -> Exception
System.out.println("Nhap y = ");
int y = scan.nextInt();
int z = x / y;//Exception -> Trong TH: y = 0 -> Chia cho 0
System.out.println("Ket qua: " + z);
int[] t = new int[3];//length = 3, index: 0 -> 2
t[1] = 100;
t[0] = 10;
t[2] = 99;
t[5] = 1000; //exception -> index = 5 -> khong nam trong pham vi dai index: 0 -> 2 -> scrash
System.out.println("t[5] = " + t[5]);
* */
//Exception -> Logic -> Xu ly nhu the nao???
//Code lai cho dung -> Sua lai code -> root cause
/**Scanner scan = new Scanner(System.in);
System.out.println("Nhap x = ");
int x = scan.nextInt(); //Nhap ky tu ko phai so -> Exception
System.out.println("Nhap y = ");
int y = scan.nextInt();
if(y != 0) {
int z = x / y;//Exception -> Trong TH: y = 0 -> Chia cho 0
System.out.println("Ket qua: " + z);
} else {
System.out.println("Error -> Khong chia dc cho 0");
}
int[] t = new int[3];//length = 3, index: 0 -> 2
t[1] = 100;
t[0] = 10;
t[2] = 99;
if(t.length > 5) {
t[5] = 1000; //exception -> index = 5 -> khong nam trong pham vi dai index: 0 -> 2 -> scrash
System.out.println("t[5] = " + t[5]);
} else {
System.out.println("Index: 0 -> " + t.length + " < 5");
}*/
//block code -> try .. catch .. finally -> Ko su dung cach nay -> TEST -> Hieu cach dung -> Ap dung
Scanner scan = new Scanner(System.in);
System.out.println("Nhap x = ");
int x = scan.nextInt(); //Nhap ky tu ko phai so -> Exception
System.out.println("Nhap y = ");
int y = scan.nextInt();
try {
int z = x / y;//Exception -> Trong TH: y = 0 -> Chia cho 0
System.out.println("Ket qua: " + z);
} catch(ArithmeticException e) {
System.out.println("Chuong trinh bi crash (ArithmeticException)");
} catch(Exception e) {
System.out.println("Chuong trinh bi crash (Exception)");//log -> save text
} finally {
System.out.println("Finish -> Giai phong tai nguyen sau khi xu ly logic");
}
int[] t = new int[3];//length = 3, index: 0 -> 2
t[1] = 100;
t[0] = 10;
t[2] = 99;
try {
t[5] = 1000; //exception -> index = 5 -> khong nam trong pham vi dai index: 0 -> 2 -> scrash
System.out.println("t[5] = " + t[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Chuong trinh bi crash (ArrayIndexOutOfBoundsException)");
} catch(Exception e) {
System.out.println("Chuong trinh bi crash (ArithmeticException)");
} finally {
System.out.println("Finish -> Giai phong tai nguyen sau khi xu ly logic");
}
//Custom Exception
Student std = new Student();
try {
std.setAge(-10);
} catch (AgeException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
#Running.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson07;
/**
*
* @author teacher
*/
public class Running implements IRunning{
@Override
public void onRunning() {
System.out.println("Trien khai code tai day");
}
}
#Student.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson07;
/**
*
* @author teacher
*/
public class Student implements IRunning{
int age;
public Student() {
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException {
if(age > 0) {
this.age = age;
} else {
// System.out.println("Yeu cau age > 0");
//Chu dong -> crash program -> dev kiem tra du lieu dau vao.
throw new AgeException();
}
}
@Override
public void onRunning() {
System.out.println("Student is running");
}
}
#Test.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson07;
/**
*
* @author teacher
*/
public class Test {
public static void main(String[] args) {
// IRunning r = new IRunning();
// Animal a = new Animal();
//Anonymous Class
// Running r = new Running(); //Khuyen khich -> Ko nen su dung cach nay -> Anonymous Class
//Trong TH -> Logic trong onRunning -> Su dung khac nhau -> Tai cac block code khac nhau
IRunning r = new IRunning() {
@Override
public void onRunning() {
System.out.println("Trien khai logic tai day");
}
};
r.onRunning();
//C1:
IRunning r1 = () -> {
System.out.println("Trien khai logic tai day");
};
r1.onRunning();
//C2:
IRunning r2 = () -> System.out.println("Trien khai logic tai day");
r2.onRunning();
IShow s1 = new IShow() {
@Override
public void showMessage(String msg) {
}
};
s1.showMessage("OKOK");
//Cach 1
IShow s2 = (String msg) -> {
System.out.println("Trien khai logic tai day > " + msg);
};
s2.showMessage("OKOK");
//Cach 2
IShow s3 = (String msg) -> System.out.println("Trien khai logic tai day > " + msg);
s3.showMessage("OKOK");
//Cach 3
IShow s4 = (msg) -> {
System.out.println("Trien khai logic tai day > " + msg);
System.out.println("Trien khai logic tai day > " + msg);
System.out.println("Trien khai logic tai day > " + msg);
};
s4.showMessage("OKOK");
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)