1. 程式人生 > 實用技巧 >linux系統建立使用者,並賦予管理員許可權

linux系統建立使用者,並賦予管理員許可權

一、異常概述與異常體系結構

  • Java程式在執行過程中所發生的異常事件可分為兩類:
  1. Error:Java虛擬機器無法解決的嚴重問題。
  2. Exception:其它因程式設計錯誤或偶然的外在因素導致的一般性問題,可以使用針對性的程式碼進行處理。例如:
  • 空指標訪問
  • 試圖讀取不存在的檔案
  • 網路連線中斷
  • 陣列角標越界
一、異常體系結構

java.lang.Throwable
	|--java.lang.Error:一般不編寫針對性的程式碼進行處理
	|--java.lang.Exception:可以進行異常的處理
		|--編譯時異常(checked)
			|--IOExecption
				|--FileNotFoundException
			|--ClassNotFoundException
		|--執行時異常(unchecked)
			|--NullPointerException
			|--ArrayIndexOutOfBoundsException
			|--ClassCastException

二、異常的處理

異常的處理:抓拋模型

過程一:“拋”,程式在正常執行的過程中,一旦出現異常,就會在異常程式碼處生成一個對應異常類的物件。
	並將此物件丟擲
	一旦丟擲物件以後,其後的程式碼就不會執行
	
	關於異常物件的產生:1. 系統自動生成的異常物件
					2. 手動的生成一個異常物件,並丟擲(throw)

過程二:“抓”,可以理解為異常的處理: 
	1.try-catch-finally 
	2.throws
	

2.1、try catch finally

異常處理的方式一:
    
try{
	//可能出現異常的程式碼
}catch(異常型別1 變數名1){
	//處理異常的方式1
}catch(異常型別2 變數名2){
	//處理異常的方式2
}finally{
	//一定會執行的程式碼,finally可選的
}   
@Test
public void test3(){

    String str = "abc";
    
    try {
        int num = Integer.parseInt(str);
        System.out.println("hello---------1");
    } catch (NumberFormatException e) {
        System.out.println("數值出現異常,已經處理完成");
    } finally {
        System.out.println("finally一定會執行,finally是可選的");
    }

    System.out.println("hello------------2");

}
說明:
try-catch    
	1.使用try將可能出現異常程式碼包裝起來,在執行過程中,一旦出現異常,就會生成一個對應異常類的物件,根據此物件的型別,去catch中進行匹配
	2.一旦try中的異常物件匹配到某一個catch時,就進入catch中進行異常的處理,一旦處理完成,就跳出當前的try-catch結構。繼續執行其後的程式碼
	3.catch中的異常型別如果沒有子父類關係,則誰宣告在上,誰宣告在下無所謂。
	4.catch中的異常型別如果滿足子父類關係,【則要求子類一定宣告在父類的上面。否則,報錯】   
	5.常用的異常物件處理的方式:
    	1) String getMessage()   //列印錯誤資訊
    		System.out.println(e.getMessage());
    	2) PrintStackTrace()     //會在控制檯報紅色 當前錯誤
    		e.PrintStackTrace();  
	6.在try結構中宣告的變數,出了try結構以後,就不能再被呼叫    
    7.try-catch-finally 可以進行巢狀    
        
/********************************************************/
        
finally
    1.finally 是可選的。
	2.finally 中宣告的是一定會被執行的程式碼。即時catch中又出現異常了,try中有return 語句,catch中有return語句等情況
    3.像資料庫連線,輸入輸出流、網路程式設計Socket等資源。JVM是不能自動的回收的,我們需要自己手動的進行資源的釋放,此時的資源釋放,就需要宣告在finally中。
    
    @Test
    public void test2(){
        File file = new File("hello.text");

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);

            int read = fis.read();

            while(read != -1){
                System.out.println((char)read);
                read = fis.read();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

/*********************************************************/    
    
體會一:使用try-catch-finally處理編譯時異常,使得程式在編譯時就不在報錯,但是執行時扔可能報錯。相當於我們使用try-catch-finally將一個編譯時可能出現的異常,延遲到執行時出現 
    
體會二:開發中,由於執行時異常出現之後我們會進行程式碼的修改,通常就不針對執行時異常編寫try-catch-finally了,只針對於編譯時異常。

2.2、throws


異常處理的方式二:throws + 異常型別

    
 public static void main(String[] args){
    try{
        method2();
    }catch(FileNotFoundException e){
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
}
public static void method2() throws FileNotFoundException,IOException{
    method1();
}
public static void method1() throws FileNotFoundException,IOException{
    File file = new File("hello.text");
    FileInputStream fis = new FileInputStream(file);
    int read = fis.read();
    while(read != -1){
        System.out.println((char)read);
        read = fis.read();
    }
    fis.close();
}


1."throws + 異常型別" 寫在方法的宣告處。指明此方法執行時,可能會丟擲的異常型別。一旦當方法體執行時,出現異常,仍會在異常程式碼處生成一個異常類的物件,此物件滿足throws異常型別時,就會丟擲。異常程式碼後續的程式碼,就不會再執行

2.體會:try-catch-finally:真正的將異常給處理掉了,
	   throws 的方式只能是將異常拋給方法的呼叫者。並沒有真正將異常處理掉

3.方法的重寫規則之一:
    子類重寫的方法丟擲的異常型別不大於父類重寫的方法丟擲的異常型別
    
    public class ExceptionOverride {
        public static void main(String[] args) {
            ExceptionOverride over = new ExceptionOverride();

            over.display(new SuberClass());
            /*
                這裡出現了多型,SuperClass s = new SubClass();
                你想本來父類中已經解決了異常,這是父類的物件引用了子類(多型),
                子類的異常大於父類的異常,是不是出現了矛盾 
            */
        }
        public void display(SuperClass s){
            try {
                s.method();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class  SuperClass{
        public void method() throws IOException {}
    }

    class  SuberClass extends SuperClass{
        public void method() throws IOException {}
    }
    

開發中如何選擇使用 try-catch-finally 或 throws

  • 如果父類中被重寫的方法沒有throws方式處理異常,則子類重寫的方法也不能使用throws,意味著如果子類重寫的方法中有異常,必須使用try-catch-finally 方式進行處理

  • 執行的方法a中,先後又呼叫了另外的幾個方法,這幾個方法是遞進關係執行的。我們建議這幾個方法使用throws的方式進行處理。而執行的方法a可以考慮使用 try-catch-finally 方式進行處理。

2.3、手動丟擲異常 throw

程式碼體現一:
public class ExceptionTest {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.regist(-1002);
    }
}

class Student{
    private int id;
    public void regist(int id){
        if(id > 0){
            this.id = id;
        }else{
            throw new RuntimeException("你輸入的資料非法"); //執行時異常
        }
    }
}


程式碼體現二:
public class ExceptionTest {
    public static void main(String[] args) {
        Student s1 = new Student();
        try {
            s1.regist(-1002);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

class Student{
    private int id;
    public void regist(int id)throws Exception{
        if(id > 0){
            this.id = id;
        }else{
            throw new Exception("你輸入的資料非法");
        }
    }
}

2.4、使用者自定義異常

如何自定義異常類?
1.繼承於現有的異常結構:RuntimeException 、 Exception
2.提供全域性常量:serialVersionUID
3.提供過載的構造器
//MyException.java
public class MyException extends RuntimeException {

    static final long serialVersionUID = -7034897190746939L; //類的表示

    public MyException(){}

    public MyException(String msg){
        super(msg);  //指向父類的構造器 RuntimeException(String msg)
    }
}

//ExceptionTest.java
public class ExceptionTest {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.regist(-2001);
    }
}

class Student{
    private int id;
    public void regist(int id){
        if(id > 0){
            this.id = id;
        }else{
            throw new MyException("不能為負數");
        }
    }
}