By GokiSoft.com| 20:39 06/07/2022|
Java Basic

[Source Code] Tìm hiểu Throw & Exception + StringBuilder & StringBufffer - C2108L

#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 lesson07;

/**
 *
 * @author Diep.Tran
 */
public class Test2 {
    public static void main(String[] args) {
        System.out.println(Calculator.chia(5, 0));;
    }
}


#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 lesson07;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) throws Exception {
        try {
            int x = 5;
            int y = 0;

            float z = x/y;

            System.out.println("z = " + z);
        } catch(Exception e) {
            e.printStackTrace();
        }
        
        try {
            int x = 5;
            int y = 0;

            float z = x/y;

            System.out.println("z = " + z);
        } catch(ArithmeticException e) {
            e.printStackTrace();
        }
        
        try {
            int[] t = new int[2]; //length: 2, index: 0 -> 1
            t[0] = 10;
            t[5] = 20;
        } catch(ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            throw new Exception();
        } finally {
            System.out.println("Ket thuc exception");
        }
        
        System.out.println("OKOK");
    }
}


#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;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //String
        String name = "TRAN VAN A";
        System.out.println("name = " + name);
        
        //StringBuilder
        StringBuilder builder = new StringBuilder();
        builder.append("vi du 1");
        builder.append("vi du 2");
        builder.append("vi du 3");
        builder.append("vi du 4");
        builder.append("vi du 5");
        
        System.out.println(builder.toString());
        
        //StringBuffer
        StringBuffer buffer = new StringBuffer();
        buffer.append("vi du 1");
        buffer.append("vi du 2");
        buffer.append("vi du 3");
        buffer.append("vi du 4");
        buffer.append("vi du 5");
        
        System.out.println(buffer.toString());
        
    }
}


#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 chia(float x, float y) {
        if(y == 0) {
            throw new ArithmeticException("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)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó