異常機制+java里的占位符如%d %f/。。。


占位符

Java中,%d和%f分別用來表示輸出時,替換整型輸出和浮點型輸出的占位符。

如:

int a=10;

float b = 23.4f;

System.out.printf("整數是:%d,小數是:%f",a,b);

輸出結果是:整數是:10小數是:23.4

異常機制

三種情況:

import java.util.InputMismatchException;
import java.util.Scanner;
/**
 * 1,未發生異常
 * 		try模塊正常執行
 * 		執行完以后執行try catch后面的代碼塊
 * 2,發生了catch 捕獲的異常
 * 		try模塊剩余的代碼將不再執行
 * 		執行對應的catch模塊
 * 		繼續執行try catch 后面的代碼
 * 3,發生了未捕獲的異常
 * 		try 模塊剩余的代碼不再執行
 * 		程序直接崩潰
 * 
 * @author tang
 *
 */
public class Test01 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("請輸入被除數");
		int n1 = 0;
		try {
			//可能會產生異常的代碼塊
			n1 = input.nextInt();
			System.out.println("被除數輸入成功");
		} catch (InputMismatchException e) {
			//一旦發生了異常  並且匹配成功  要么就把異常的信息打印出來   要么就寫入到日志文件
			e.printStackTrace();
			System.out.println("輸入的必須是數字");
		} 
		
		System.out.println("程序結束");
		
	}
}

 

當interger創建對象時傳入不是數字類型的字符時,會報numberFormatException!!

聲明多個異常用逗號隔開!!

 

 

finally關鍵字

 


public class FinallyDemo {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("請輸入被除數");
		int n1 = 0;
		int n2 = 0;
		try {
			//可能會產生異常的代碼塊
			n1 = input.nextInt();
			System.out.println("請輸入除數");
			n2 = input.nextInt();
			System.out.println(n1/n2);
		} catch (InputMismatchException e) {
			//一旦發生了異常  並且匹配成功  要么就把異常的信息打印出來   要么就寫入到日志文件
			//e.printStackTrace();
			System.out.println("輸入的必須是數字");
			return;
		} catch (ArithmeticException e) {
			System.out.println("發生了數學運算的異常");
		} catch (Exception e) {
			System.out.println("發生了未知異常");
		}finally{
			//finally 這個代碼塊  不管程序是否發生異常  總是會執行
			//一般做一些關閉數據庫連接   關閉io流等一些必須的操作
			//System.exit(0);  只有手動退出虛擬機的時候   程序不會執行finally模塊
			//當代碼塊中有return的時候   先執行finally  再return
			System.out.println("finally");
		}
		
		System.out.println("程序結束");
	}
}

 

 

 

throws關鍵字

/**
 * throws   當一個方法內發生了異常
 * 			但是無法解決異常,則可以使用throws 將異常拋出    
 * 			哪里調用了該方法,哪里就獲取到該異常
 * @author tang
 *
 */
public class TestThrows {
	public static void main(String[] args) throws InterruptedException {
		test();
	}
	
	public static void test() throws InterruptedException {
		Thread.sleep(1000);
	}
}

 throw關鍵字(自定義異常對象的描述信息!)

public class TestThrow {
	public static void main(String[] args) {
		Person wangcai = new Person();
		try {
			wangcai.setGender("ladyboy");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class Person{
	private String name;
	private String gender;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) throws Exception {
		if ("男".equals(gender) || "女".equals(gender)) {
			this.gender = gender;
		}else {
			Exception ex = new Exception("性別只能是男或女");
			throw ex;
		}
	}
	
}

 自定義異常

 

public class GenderException extends Exception{

	public GenderException() {
		super();
	}

	public GenderException(String arg0) {
		super(arg0);
	}
	
}
public class AgeException extends Exception{

	public AgeException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public AgeException(String arg0) {
		super(arg0);
	}
	
}

 

public class Person {
	private String name;
	private String gender;
	private int age;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) throws GenderException {
		if ("男".equals(gender) || "女".equals(gender)) {
			this.gender = gender;
		}else {
			GenderException ex = new GenderException("性別只能是男或女");
			throw ex;
		}
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) throws AgeException {
		if (age > 0 && age < 200) {
			this.age = age;
		}else{
			AgeException ex = new AgeException("年齡必須是0~200");
			throw ex;
		}
	}
	
	
}

  還可以這樣制造異常

public class Test {
	public static void main(String[] args) {
		Person wangcai = new Person();
		wangcai.setName("旺財");
		try {
			wangcai.setAge(300);
			wangcai.setGender("ladyboy");
		} catch (AgeException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GenderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM