By GokiSoft.com|
16:53 01/03/2021|
Java Basic
[Share Code] Tìm hiểu về Exception + StringBuilder + StringBuffer - Lập trình Java basic
#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 lesson08;
import java.util.Scanner;
/**
*
* @author DiepTV
*/
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int[] t = new int[3];
//length: 3, index: 0 -> 2
t[0] = 7;//Dung -> ko gap van de j
// t[3] = 10;
// System.out.println("t[3] = " + t[3]);
System.out.println("Nhap x: ");
int x = scan.nextInt();
System.out.println("Nhap y: ");
int y = scan.nextInt();
/**int s = x/y;
System.out.println("s: " + s);*/
//Gap thuong xuyen => ko tranh khoi duoc
//Exception: business logic => kiem soat lai code => fix chinh xac => exception
//Exception: khong kiem soat dc -> ket noi mang, thao tac ve (file ko ton tai, phan quyen file (r, w, a))
//Khong kiem soat -> bo qua -> bo qua exception -> chuong trinh van chay bt hoac dua ra thong bao.
/**System.out.println("Nhap x: ");
x = scan.nextInt();
System.out.println("Nhap y: ");
y = scan.nextInt();*/
/**if(y == 0) {
System.out.println("Ko duoc phep chia cho 0 -> kiem tra du lieu");
} else {
s = x/y;
System.out.println("s: " + s);
}*/
/**try {
//Bat dau xu ly
//Khoi tao rat nhieu tai nguyen => yeu cau can xoa di => sau khi su dung xong.
s = x/y;
System.out.println("s: " + s);
//Ket thuc xu ky ko gap exception
//Neu dat xoa tai nguyen o day => miss => neu nhu xay ra exception => khong goi dc.
// x = 0;
// y = 0;
// s = 0;
// System.out.println("Xoa thanh cong.");
} catch(Exception e) {
//Khi trong try -> xay ra exception
System.err.println("Error...");//Biet exception => message cho nguoi dung biet.
} finally {
x = 0;
y = 0;
s = 0;
System.out.println("Xoa thanh cong.");
//Gap exception hay ko -> deu goi vao khoi code nay
System.out.println("finish");
//Do vay de an toan => se la noi de xoa toan bo tai nguyen
}*/
try {
int s = x/y;
t[3] = 10;
} catch(ArithmeticException e) {
System.out.println("Error > ArithmeticException ...");
e.printStackTrace();
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Error > ArrayIndexOutOfBoundsException ...");
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
System.out.println("TH ko biet nguyen nhan loi do dau -> bat loi va kiem tra.");
} finally {
System.out.println("Ket thuc");
}
throw new Exception("Test throws");//chuong trinh dang chay -> die.
//business logic => die => dev => biet fix.
// System.out.println("Finish program");
}
public void test() {
Student std = new Student();
std.input();
}
/**
* inner class => thong thuong no la cac class trong class cha.
*/
public class Student {
String name, address;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void input() {
System.out.println("Nhap ...");
}
}
}
#Test.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 lesson08;
/**
*
* @author DiepTV
*/
public class Test {
public static void main(String[] args) {
String s1 = "ABC";
// s1 = new String("Xin chao");//Khong nen khai bao bien theo cach nay.
System.out.println("s1: " + s1);
s1 = s1 + " XYZ";
s1 = s1.concat("OKOK");
System.out.println("s1: " + s1);
//Quan ly du lieu string phuc tap
/**
* Add du lieu => tu dong mo rong khong gian bo nho => cham hon buffer => quan ly tai nguyen tot hon buffer
*/
StringBuilder builder = new StringBuilder();
builder.append("A1");
builder.append("A2");
builder.append("A3");
builder.append("A4");
builder.append("A5");
builder.append("A6");
//insert, delete, ...
System.out.println(builder.toString());
/**
* Quan ly nhanh => ton tai nguyen
*/
StringBuffer buffer = new StringBuffer();
buffer.append("B1");
buffer.append("B2");
buffer.append("B3");
buffer.append("B4");
buffer.append("B5");
buffer.append("B6");
System.out.println(buffer.toString());
}
}
#Test2.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 lesson08;
import java.util.Scanner;
/**
*
* @author DiepTV
*/
public class Test2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap x: ");
float x = scan.nextInt();
System.out.println("Nhap y: ");
float y = scan.nextInt();
float s;
try {
s = Calculator.chia(x, y);
System.out.println("s: " + s);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Hoan thanh code");
}
}
#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 lesson08;
/**
*
* @author DiepTV
*/
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 Exception {
if(y == 0) {
throw new Exception("Loi chia cho 0 -> Vui long sua loi nay!!!");
}
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)