JAVA笔记9__异常/throw关键字/自定义异常/受检与非受检异常、assert关键字/StringBuffer、StringBuilder/代码国际化、动态文本

/**
 * 异常:在程序中导致程序中断运行的一些指令
 * 1.受检异常:编译期
 * 2.非受检异常:运行期
 * 异常处理过程分析:
 * 1.一旦产生异常,系统会自动产生一个异常类的实例化对象
 * 2.此时如果存在对应try语句,则执行,否则程序将退出,并由系统报告错误
 * 
 */
public class Main {
    public static void main(String[] args) {
        /*
        try{
            //有可能发生异常的代码段
        }catch(异常类型对象){
            //异常的处理操作
        }catch(异常类型对象){
            //异常的处理操作
        }...
        finally{
            //异常的统一出口
        }
        */
        int a = 100;
        Scanner input = new Scanner(System.in);
        try{
            for(int i=0;i<10;++i){
                int b = input.nextInt();
                int c = a / b;
                System.out.println(c);
            }
        }catch(ArithmeticException e){
            System.out.println("算术运算异常");
            e.printStackTrace(); //输出栈中的异常信息
        }catch(Exception e){
            System.out.println("运算异常");
        }finally{ //用途:资源的释放
            System.out.println("finally:不管try中的语句是否出现异常都会执行");
        }
        
        div(10,2);
    }
    
    public static int div(int a,int b){
        int res=0;
        try{
            res=a/b;
            return res; //执行完finally里的东西才执行这句
        }catch(ArithmeticException e){
            e.printStackTrace();
            return 0;
        }finally{
            System.out.println("除法运算结束");
        }
    }
}
public class Main {
    public static int add() throws InputMismatchException{ //抛出,让上级去处理
        Scanner input = new Scanner(System.in);
        try{
            int a = input.nextInt();
            int b = input.nextInt();
            return a+b;
        }catch(InputMismatchException e){
            //throw new InputMismatchException("There is a mistake.");
            throw e;
        }finally{
            System.out.println("All finished.");
        }
    }
    public static void main(String[] args) {
        try{
            System.out.println(add());
        }catch(InputMismatchException e){ //上级来处理
            e.printStackTrace();
        }
    }
}
/**
 * 自定义异常类:可以通过继承Exception类或已有的异常类  例:某些业务逻辑不合理则可定义成异常
 */
public class Main {
    public static int count = 5; //打飞机的次数
    public static void play() throws LogicException{
        /**
         * 打飞机游戏
         */
        Scanner input = new Scanner(System.in);
        while(true){
            System.out.print("请输入非零数字:");
            int yesno = input.nextInt();
            if(0==yesno){
                System.out.println("退出游戏");
                return;
            }
            if(0==count){
                throw new LogicException("你的飞机已打光!");
            }
            System.out.println("你还有"+(--count)+"架飞机");
        }
    }
    public static void main(String[] args) {
        try{
            play();
        }catch(LogicException e){
            e.printStackTrace();
        }
    }
}

class LogicException extends Exception{
    public LogicException(){
        super();
    }
    public LogicException(String msg){
        super(msg);
    }
}
/**
 * 受检与非受检异常:在编译期是否受检查
 * 受检异常必须用throws关键字,并且要用try,非受检可以不用
 * 
 * assert关键字:表示断言,当程序执行到某个固定位置时,程序中某个变量的取值肯定是预期的结果,那么这种操作可以使用断言完成
 * 操作语法:assert 表达式
 */
public class Main {
    public static void main(String[] args) {
        int x = 11;
        assert x==10:"结果不正确"; //需要启动断言,加VM参:-ea
    }
}
/**
 * #快捷键:按住Ctrl,然后点类名,可以查看原代码
 * 使用String连接字符串,代码性能会非常低,因为String的内容不可改变(连接时不断创建新的char数组)
 * 解决方法:StringBuffer
 * StringBuffer的兄弟:StringBuilder(比StringBuffer简单些,速度更快些,建议优先使用)
 * 区别:StringBuilder字符串缓冲区被单个线程使用,StringBuffer是多线程,具体百度吧。。。
 */
public class Main {
    public static void main(String[] args) {
        String s = "fish7";
        StringBuffer sb = new StringBuffer();
        sb.append("此刻打盹,你将做梦。");
        sb.append("此刻学习,你将圆梦。");
        System.out.println(sb);
        //具体的各种方法查java手册
    }
}
/**
 * 代码国际化:Locale类、ResourceBundle类
 * 具体百度吧。。。。
 * 
 * 动态文本:欢迎你,某某某(不确定)  用到MessageFormat类
 */
public class Main {
    public static void main(String[] args) {
        Locale l1 = new Locale("zh","CN");
        Locale l2 = Locale.CANADA;
        Locale l3 = Locale.getDefault(); //当前系统环境
        
        //通过ResourceBundle工具类绑定资源文件(包名 文件名的基本名称)
        ResourceBundle r1 = ResourceBundle.getBundle("pkg1.info",l1);
        
        //从绑定的资源文件(属性文件)中根据key获取对应的value
        Scanner input = new Scanner(System.in);
        String inputKey = r1.getString("input");
        String userName = r1.getString("userName");
        
        
    }
}
原文地址:https://www.cnblogs.com/fish7/p/4147721.html