美文网首页
JavaSE 异常

JavaSE 异常

作者: 23b57d72cde7 | 来源:发表于2018-04-09 19:20 被阅读0次

    1.异常 Exception
    异常是由系统或人为不正规操作导致系统不能再继续运行
    错误大部分由硬件导致的系统问题 蓝屏 坏道 主板老化
    异常不能避免 需要捕获和处理
    错误可以避免
    try catch finally throws throw
    抓 处理 最终执行 申明抛出 抛出
    语法格式:
    try{
    // 可能抛出异常的代码

    }catch(异常类型对象 e){
    // 匹配对应的异常进行处理

    }finally{
    // 不管是否发生异常都会执行这段代码
    }
    常见的异常:
    NullPointerException 空指针异常
    ArrayIndexOutOfBoundsException 数组下标越界异常
    ClassNotFoundException 找不到类异常
    FileNotFoundException 找不到文件异常
    SQLException sql语句执行错误
    IOExceprion 流所产生的异常
    NumberFormatException 数字格式化异常 字符串转数
    finally 执行总是在 return 之前

    处理异常的两种方式: 抓 抛
    try catch 抓
    throws throw 抛
    底层抛,顶层抓
    包装类
    基本数据类型的类的展现方式
    特点:有利于开发中数据相互转换
    除了 int char 其他的都是首字母变大写
    int Integer
    char Character
    基本数据类型= 》String
    .toString();
    String =》基本数据类型
    包装类.parseXXX();
    String =>包装类型
    包装类.valueOF();
    包装类型=》基本数据类型
    对象引用.XXXValue();
    基本数据类型=》包装类型
    new 包装类型(基本数据类型);
    valueOF(基本数据类型);
    自动装箱
    基本数据类型可以直接赋值给包装类
    Integer i = 1;
    自动拆箱
    将包装类转换为基本数据类型
    int j = i;

    • 作业:
    package com.sxt;
    
    public class work001 {
            
        public static void main(String[] args) {
                //1.使用try catch 捕获除0异常,并打印信息“不能除0
                try {
                    int i =  10 / 0;
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("不能除〇");
                }
            
        //2.将字符串“12.5” 转换为float类型
        String s = "12.5";
        float a = Float.parseFloat(s);
        System.out.println(a);
            //3.将int类型的127转换成 byte和short类型
            int i = 127;
            Byte b = new Byte((byte)i);
            Short ss = new Short((short)i);
            System.out.println("byte是"+b+"\t short是"+ss);
            
        }
    }
    

    相关文章

      网友评论

          本文标题:JavaSE 异常

          本文链接:https://www.haomeiwen.com/subject/hvxwhftx.html