java学习笔记(4)

异常处理:要将范围相对较小的异常放在前面、范围相对大的异常放在后面

try
        {
            int[] a=new int[5];
            a[6]=0;
        }

        catch(ArrayIndexOutOfBoundsException ee)
        {
            System.out.println("发生了数组下标越界异常");
        }
        catch(Exception e)
        {
            System.out.println("其他exception occured:"+e);
        }
        finally
        {
            System.out.println("finally中的语句肯定执行");
        }

运行结果: finally语句无论程序中是否发生异常都被执行

try...catch...finally语句:

一、当不存在catch时,finally必须存在且紧跟try(catch和finally两者可以有一个不存在);

二、try-catch-finally语句间不能存在任何语句(除注释)

 

throws/throw 关键字:

如果一个方法没有捕获到一个检查性异常,那么该方法必须使用 throws 关键字来声明。throws 关键字放在方法签名的尾部。

也可以使用 throw 关键字抛出一个异常,无论它是新实例化的还是刚捕获到的。

throws:声明这个方法会抛出这种类型的异常

throw:抛出一个异常:新实例化的/刚捕获到的

package com.test;

import java.io.IOException;

class HelloException extends Exception
{
    public HelloException()
    {
    }
    public HelloException(String s)
    {
        super(s);
    }
}

public class exc {
    public void withdraw()throws IOException, HelloException
    {
        System.out.println("hello");
        throw new HelloException("发生了hello异常");
    }
    public static void main(String[] args)throws IOException
    {
        exc ex=new exc();
        try
        {
            ex.withdraw();
        }
        catch (IOException m)
        {
            throw m;
        }
        catch(HelloException h)
        {
            System.out.println(h);
        }
    }
}

自定义异常:

package com.test;

class HelloException extends Exception
{
    public HelloException()
    {
    }
    public HelloException(String s)
    {
        super(s);
    }
}
class deFen
{
    public String name;
    public int grade;
    public deFen(){}
    public deFen(String na,int gar)throws HelloException
    {
        this.name=na;
        this.grade=gar;
        if(gar>=0&&gar<=100) ;
        else
            throw new HelloException(name+"分数出错");
    }
}
public class exc {

    public static void main(String[] args)
    {
        deFen d1;
        deFen d2;
        try
        {
            d1=new deFen("jack",87);
            d2=new deFen("cheater",102);
        }
        catch (HelloException he)
        {
            System.out.println("发生异常:"+he);
        }
    }
}

运行结果

 

tips:子类被重写(Override)方法不能比父类被重写方法声明(throws)更多的异常

 

 

内部类:

在外部类外访问内部类:要先创建外部类对象然后以外部类对象为标识来创建内部类对象

package com.test;
class outer
{
    class inner
    {
        int i=1;
        int j=2;
    }
}
public class innerClass
{
    public static void main(String[] args)
    {
        outer.inner i1=new outer().new inner();
        System.out.println("Method "+i1.i);

        outer o1=new outer();
        outer.inner i2=o1.new inner();
        System.out.println("Method "+i2.j);
    }
}

在内部类中访问外部类:是没有限制的

在内部类直接访问时(对于同名的成员变量),将访问的时内部类的成员变量;若想访问外部类,就要先创建一个外部类对象,然后使用该对象调用外部类成员变量。

使用this也可以指向当前对象的引用,完成内部类访问同名成员变量

package com.test;
class outer
{
    int i=100;
    class inner
    {
        int i=1;
        outer o=new outer();
        public void myVoid()
        {
            System.out.println(i);               //   1
            System.out.println(o.i);             //   100
            System.out.println(this.i);          //   1
            System.out.println(outer.this.i);    //   100

        }
    }
}
public class innerClass
{
    public static void main(String[] args)
    {
        outer.inner i1=new outer().new inner();
        i1.myVoid();
    }
}

运行结果:

 

局部内部类:

内部类作为局部成员出现:只能再它所在的方法中进行调用

class Wai
{
    public void myVoid()
    {
        class Nei
        {
            int i=5;    
        }
        Nei n=new Nei();
        System.out.println(n.i);
    }
}

在局部内部类中可以直接调用外部类成员变量;在局部类中不能访问普通的局部变量,必须将该局部变量声明为final

静态方法中的局部内部类:静态方法中的局部内部类可以访问静态成员变量

静态内部类:

在外部类中访问静态内部类:要先创建内部类对象,再调用内部类成员

在外部类外访问静态内部类:可以直接创建内部类对象

Wai.Nei wn=new Wai.Nei();

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值