异常 数组 10

一、异常(Throwable)

1.理解

Throwable--------->Error||Exception

异常:程序无法正常执行完毕。

异常的文类:运行时|检查时

2.Error

Error:这类不需要程序员关注,一般是由虚拟机生成并脱出的

3.Exception(异常)

1)RuntimeException:运行时异常,运行期间发生的异常,编译时没有问题

可以通过增强程序的健壮性来处理 if..else

常见的运行时异常:

  • 空指针异常:NullPointerException :当对象无法正常存在使用,会发生空指针

  • 类型转换异常: ClassCastException -->intansceof

  • 数组下标越界

  • 数组长度负数异常

  • 数学异常 :ArithmeticException

  • 数字转换异常: NumberFormatException

2)CheckedException:检查时异常|编译时异常:发生在编译期间

如果不处理,程序无法运行。

4、异常处理

1)throws:异常抛出,抛给上一层处理,当前不处理

2)try......catch 异常捕获

try{
        可能会出现异常的代码;
    }catch(NullPointerException e){ //捕获空指针类型的对象,或者多态
        如果出现这个异常,执行的代码...
    }catch(FileNotFoundException e){
        如果出现这个异常,执行的代码...
    }....
     catch(Exception e){
        任意类型的异常都可以捕获
    }finally{
        无论try中是否出现异常,都会执行finally中的代码
    }
  

try一旦弄到异常,try后面的代码不执行,直接去找对应得catch,如果后面有很多catch,就依次从上到下判断。一个try后面可以跟多个catch,至少一个catch。

如果发现多个catch,捕获异常类型范围要从小到大。

public class Exception04 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			System.out.println("开始。。。。。。。。。。");
			int i=5/0;//数子异常
			String str=null;//空指针异常
			System.out.println(str);
			FileInputStream file=new FileInputStream("F://bin");//文件找不到异常
			
		}catch(ArithmeticException e) {
			//打印出来
			System.out.println("数字异常。。。。");
			e.printStackTrace();
			System.out.println(e.getMessage());
		}catch(NullPointerException e) {
			System.out.println("空指针。。。。");
			e.printStackTrace();
		}catch(FileNotFoundException e) {
			System.out.println("文件找不到。。。。。");
			e.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
			System.out.println("异常处理。。。");
		}finally {
			System.out.println("一般资源最好的地方放这里");
		}
		System.out.println("结束。。。。。。。。");
	}
	
}

 

5.自定义异常

throw 制造异常

前提:异常对象的类型需要继承自Throwable

自定义异常:

  • 直接或者间接的继承自Exception

  • 运行时异常: 继承自RuntimeException

  • 编译时异常: Exception或者某一个编译时异常的子类

/**
    编译时异常:密码要大于等于6位数
*/
public class Exception05 {
​
    public static void main(String[] args){
        // TODO Auto-generated method stub
        Info in=new Info();
        String name="xiaoxiao";
        String pwd="123";
        in.setUname(name);
        try {
            in.setUpwd(pwd);
        } catch (StringException e) {
            // TODO Auto-generated catch block
            try {
                in.setUpwd("123456");
            } catch (StringException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        System.out.println(in.toString());
    }
    
}
class Info{
    private String uname;
    private String upwd;
    //setter 和 getter 访问器
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) throws StringException {
        if(upwd.length()>=6) {
            this.upwd = upwd;
        }else {
            throw new StringException(upwd+"密码要大于六位");
        }
        
    }
    @Override
    public String toString() {
        return "Info [uname=" + uname + ", upwd=" + upwd + "]";
    }
    
}
//定义编译异常
​
class StringException extends Exception{
    public StringException() {
        //super();
    }
    public StringException(String mesgg) {
        super(mesgg);
    }
}
​
/**
    运行异常:密码大于等于6位数
*/
public class Exception06 {
​
    public static void main(String[] args){
        // TODO Auto-generated method stub
        Info2 in=new Info2();
        String name="xiaoxiao";
        String pwd="123";
        in.setUname(name);
        if(pwd.length()>=6) {
            in.setUpwd(pwd);
        }else {
            in.setUpwd("123456");
        }
        System.out.println(in.toString());
    }
}
class Info2{
    private String uname;
    private String upwd;
    //setter 和 getter 访问器
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd){
        if(upwd.length()>=6) {
            this.upwd = upwd;
        }else {
            throw new StringException(upwd+"密码要大于六位");
        }
        
    }
    @Override
    public String toString() {
        return "Info [uname=" + uname + ", upwd=" + upwd + "]";
    }
    
}
//定义运行异常
class StringException extends RuntimeException{
    public StringException() {
        //super();
    }
    public StringException(String mesgg) {
        super(mesgg);
    }
}
​

二、数组(Array)

1.理解

1)数组:存储多个数据

2)在内存中是一段连续的内存空间

3)相同数据类型数据的有序集合

2.特点

  • 数组是一个容器,是一个引用数据类型,堆中

  • 数组中的所有数据要求数据类型相同

  • 长度一旦确定不可改变

  • 有序 根据序号使用(索引|下标)

3.数组声明

数据类型 [] 数组名;

数据类型:数组所有数据的数据类型

[]------>

数组名:标识符

//定义静态数组初始化
        int [] arr=new int[] {1,2,3,4,5,6,7,8,9,10};
        //定义动态数组初始化
        int [] arr2=new int[10];

4.数组遍历

数组的遍历:依次的拿出数据的中每一个数据操作

1.for普通for循环

2.增强for循环:for..each

  • for(数据类型 变量名 : 数组名|容器名){

  • 变量名 ->代表数组中的从前到后的每一个数据

    }

//增强for
        for(int i:arr) {
            //System.out.println(i);
        }
        System.out.println(Arrays.toString(arr));
        //普通for
        for(int i=0;i<arr.length;i++) {
            //System.out.println(arr[i]);
            
        }
        System.out.println(Arrays.toString(arr));

5.数组常用方法

  • Arrays.toString(数组) 把数组中的内容以字符串的形式返回

  • Arrays.equals(arr1, arr2) 比较两个数组内容是否相等

  • --从索引为0的位置开始

  • static int[] copyOf(int[] original, int newLength) 拷贝,截取,填充的效果

  • --从索引为指定的位置开始

  • static int[] copyOfRange(int[] original, int from, int to) 从原数组的指定位置开始拷贝,拷贝到指定to位置

  • 注:结束位置不包含

  • static void fill(int[] a, int val) 数组中的所有位置,使用val数值填充

  • static void fill(int[] a, int fromIndex, int toIndex, int val)

  • static void sort(int[] a) 对指定的 int 型数组按数字升序进行排序。

  • static int binarySearch(int[] a, int key) 查找数据出现的索引 返回值: -插入点 - 1

  • 使用前提:先升序排序

int[] arr4= {1,2,3,4,5};
        int[] arr= {1,2,3,4,5};
        //把数组的内容以字符串来输出
        System.out.println(Arrays.toString(arr4));
        //比较二个数组相等equals
        System.out.println(Arrays.equals(arr4, arr));
        //数组copyof拷贝长度截取是索引为0开始
        System.out.println(Arrays.toString(Arrays.copyOf(arr, 3)));
        //索引指定位置进行截取
        System.out.println(Arrays.toString(Arrays.copyOfRange(arr, 1, 3)));
        //全填充数据,为10
        Arrays.fill(arr, 10);
        System.out.println(Arrays.toString(arr));
        //截取数来填充
        Arrays.fill(arr4, 1,3, 8);
        System.out.println(Arrays.toString(arr4));
        //进行排序
        Arrays.sort(arr4);
        System.out.println(Arrays.toString(arr4));
        //查找数组的索引
        System.out.println(Arrays.binarySearch(arr4, 3));

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值