By GokiSoft.com|
09:56 19/07/2021|
Java Basic
[Share Code] Exception + Package + Base64 - Lập trình Java - C2010G
Nội dung kiến thức:
- Exception (Try .. Catch .. Finally) -> Crash
- Logic: chia cho 0, ArrayIndexOutoff boundnary, ..., null, ...
- System: Memory, Thiet bi phan chung
- Middle Ware: Network, ...
- Third Party: API, ...
- Package trong Java
- Mã hóa (md5, base64, ...)
==============================================
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson07;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import lesson07.bt999.CTivi;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
// CTivi tivi = null;
// tivi.On();
Scanner scan = new Scanner(System.in);
System.out.println("Nhap x = ");
int x = scan.nextInt();
System.out.println("Nhap y = ");
int y = scan.nextInt();
//C1: Fix logic -> Bat buoc fix theo cach nay
/**if(y == 0) {
System.out.println("Ko dc chia cho 0");
} else {
int s = x/y;
System.out.println("Ket qua s = " + s);
}*/
try {
//Noi de code -> nghi van se gay ra chet chuong trinh
int s = x/y;
System.out.println("Ket qua s = " + s);
int[] t = new int[2];
//index: 0 -> 1, length: 2
t[1] = 12;
t[6] = 10;
System.out.println("OKOK");
} catch(ArithmeticException e) {
System.out.println("ArithmeticException ...");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException ...");
} catch(Exception abc) {
System.out.println("Exception Chet chuong trinh");
} finally {
System.out.println("Finally");
}
float ss;
try {
ss = Calculator.chia(x, y);
System.out.println("ss = " + ss);
} catch (CalculatorException ex) {
ex.showMsg();
}
//https://www.javatpoint.com/java-date-to-string
//https://www.javatpoint.com/java-string-to-date
//https://stackoverflow.com/questions/3581258/adding-n-hours-to-a-date-in-java
}
}
#CalculatorException.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson07;
/**
*
* @author Diep.Tran
*/
public class CalculatorException extends Exception{
float x, y;
public CalculatorException(float x, float y, String string) {
super(string);
this.x = x;
this.y = y;
}
public void showMsg() {
System.out.println("x = " + x + ", y = " + y + ", " + getMessage());
}
}
#Calculator.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson07;
/**
*
* @author Diep.Tran
*/
public class Calculator {
public static float cong(float x, float y) {
return x + y;
}
public static float tru(float x, float y) {
return x - y;
}
public static float nhan(float x, float y) {
return x * y;
}
public static float chia(float x, float y) throws CalculatorException {
if(y == 0) {
// System.out.println("Ko duoc phep chia cho 0");
// return 0;
throw new CalculatorException(x, y, "Ko duoc phep chia cho 0");
}
return x / y;
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)