Java try catch

/*
 
异常处理的捕捉形式:
这是可以对异常进行针对性处理的方式。
 
具体格式是:
try
{
//需要被检测异常的代码。
}
catch(异常类 变量)//该变量用于接收发生的异常对象
{
//处理异常的代码。
}
finally
{
//一定会被执行的代码。
}
 
 
异常处理的原则:
1,函数内容如果抛出需要检测的异常,那么函数上必须要声明。
否则必须在函数内用trycatch捕捉,否则编译失败。
 
2,如果调用到了声明异常的函数,要么trycatch要么throws,否则编译失败
 
3,什么时候catch,什么时候throws 呢?
功能内容可以解决,用catch。
解决不了,用throws告诉调用者,由调用者解决 。
 
4,一个功能如果抛出了多个异常,那么调用时,必须有对应多个catch进行针对性的处理。
内部又几个需要检测的异常,就抛几个异常,抛出几个,就catch几个。
class FuShuIndexException extends Exception
{
    FuShuIndexException()
    {}

    
    FuShuIndexException(String msg)
    {
        super(msg);
    }
}


class Demo
{
    public int method(int[] arr,int index)//throws NullPointerException,FuShuIndexException
    {    
        if(arr==null)
            throw new NullPointerException("没有任何数组实体");
        if(index<0)
            throw new FuShuIndexException();
            
        return arr[index];
    }
}

class  ExceptionDemo4
{
    public static void main(String[] args) 
    {
        int[] arr = new int[3];        
        Demo d = new Demo();
        try
        {
            int num = d.method(null,-1);
            System.out.println("num="+num);
            return;
        }
        
        catch(NullPointerException e)
        {
            System.out.println(e.toString());
                         return;
        }
        catch (FuShuIndexException e)
        {
            System.out.println("message:"+e.getMessage());
            System.out.println("string:"+e.toString());

            e.printStackTrace();//jvm默认的异常处理机制就是调用异常对象的这个方法。

            System.out.println("负数角标异常!!!!");
        }
        /*
        catch(Exception e)   //多catch父类的catch放在最下面。
        {
            System.out.println(e);
        }
               finally       //一定会执行的代码
        {
            System.out.println("finally");
        }
        */
        System.out.println("over");
    }
    
}


/*
try catch finally 代码块组合特点:

1,
try catch finally

2,
try catch(多个)当没有必要资源需要释放时,可以不用定义finally。

3,
try finally 异常无法直接catch处理,但是资源需要关闭。 
*/

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

class Person
{
    int age;
    String name;
    public Person(String name,int age)
    {
        this.name = name;
        this.age = age;    
    }
    
    public Person()
    {
        
    }
    
    public int method(int[] arr,int index) throws FushuException 
    {
        if(arr == null)
        {
            throw new NullPointerException("数组不能为null");
        }
        if(index < 0)
        {
            throw new FushuException("数组下标不能为负数");
        }
        if(index > arr.length)
        {
            throw new ArrayIndexOutOfBoundsException("数组下标越界 ");
        }
        return arr[index];
    }
}

public class FieldDemo 
{
    public static void main(String[] args) throws FushuException
    { 
        
        Person person = new Person("Tom", 50);
        int[] arr = {1,2,3};
        
        try {
            int rel = person.method(arr, -2);
            System.out.println(rel);
        } 
        finally
        {
            System.out.println("finally");
        }
        
        System.out.println("over");
      
    }  
  
} 

 

posted @ 2018-01-18 08:49  darrell007  阅读(265)  评论(0编辑  收藏  举报