占位符
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(); } } }