By GokiSoft.com| 20:19 22/04/2020|
Java Advanced

Share Code - Tìm hiểu Exception + Throw + Try Catch + Package

Chia sẻ source code bài hoc


1. Tìm hiểu về Exception + Crassh trong phần mềm
/*
 * 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 lession1;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        int[] t = new int[3];
        //mang => length = 3, index = 0 -> 2
        try {
            int t1, t2;
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter x: ");
            t1 = scan.nextInt();

            System.out.println("Enter y: ");
            t2 = scan.nextInt();
            
            int t3 = t1/t2;
            System.out.println("t3 = " + t3);
            
            //error => exception => crash => die
            System.out.println("Step 1");
            t[1] = 6;
            t[8] = 100;
            System.out.println("t[1] = " + t[1]);
            System.out.println("Step 2");
        } catch(ArithmeticException e) {
            e.printStackTrace();
        } catch(ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("Step 3");
        } finally {
            System.out.println("Step 4");
            //free resouce
        }
        //Business logic => 

        int x, y;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter x: ");
        x = scan.nextInt();
        
        System.out.println("Enter y: ");
        y = scan.nextInt();
        
        if(y == 0) {
            System.out.println("Error >> y = 0");
        } else {
            int result = x/y;
        
            System.out.println("result = " + result);
        }
        //Crash => Exception
        //Tim cach su ly no.
        //Exception => crash
        //System => die
        //Developer => die
    }
}

2. Tạo 1 custom exception

/*
 * 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 lession1;

/**
 *
 * @author Diep.Tran
 */
public class CalculatorException extends Exception{
    int x, y;

    public CalculatorException() {
    }

    public CalculatorException(int x, int y, String message) {
        super(message);
        this.x = x;
        this.y = y;
    }

    @Override
    public String getMessage() {
        return "x = " + x + ", y = " + y + ", message = " + super.getMessage();
    }
}



/*
 * 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 lession1;

/**
 *
 * @author Diep.Tran
 */
public class Calculator {
    public static int divide(int x, int y) throws CalculatorException {
        if(y == 0) {
            //input data => error => bao cho coder => error => y ???
            throw new CalculatorException(x, y, "by zero");
        } else {
            return x/y;
        }
    }
}


3. Debug + Logger
/*
 * 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 lession1;

import java.util.logging.Level;
import java.util.logging.Logger;
import sun.rmi.runtime.Log;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        try {
            Logger.getLogger(Test.class.getName()).log(Level.OFF, "Step 1");
            int t = Calculator.divide(6, 0);
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, "Step 2");
            System.out.println("t = " + t);
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, "Step 3");
        } catch (CalculatorException ex) {
            System.out.println(ex.getMessage());
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, "Step 4");
        }
    }
}


4. Tìm hiểu String + StringBuilder + StringBuffer
/*
 * 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 lession1;

/**
 *
 * @author Diep.Tran
 */
public class Test1 {
    public static void main(String[] args) {
        String str = "";
        str += "line 1";
        str += "line 2";
        System.out.println("str: " + str);
        
        StringBuilder builder = new StringBuilder();
        builder.append("line 1");
        builder.append("line 2");
        builder.append("line 3");
        
        System.out.println(builder.toString());
        
        StringBuffer buffer = new StringBuffer();
        buffer.append("line 1");
        buffer.append("line 2");
        buffer.append("line 3");
        
        System.out.println(buffer.toString());
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)