throws和throw assert

throws
throws :用在方法上,明确表示该方法会产生异常,但是方法内部不做处理,将异常抛给调用处。调用处必须进行异常处理。异常后面的语句不再执行。

public class Throw1
{
    public static void main(String[] args)
    {
        //主方法中调用用throws声明的方法,必须进行异常处理
        try
        {
            calu(10,0);
        }
        catch (Exception e)  //处理异常
        {
            e.printStackTrace(); 
        }
        System.out.println("********");  //会打印
    }
    public  static  int calu(int a,int b) throws Exception  //将异常抛给调用处
    {
        int result= a/b;
        System.out.println("#####"); //产生异常后,异常后面的语句不再执行
        return result;
    }
}

主方法也可以throws,是让JVM处理异常,然后主方法异常后面的语句不再执行

public class Throw1
{
    public static void main(String[] args) throws Exception  //将异常抛给JVM
    {
        calu(10,0);
        System.out.println("********");  //不会打印
    }
    public  static  int calu(int a,int b) throws Exception  //将异常抛给调用处
    {
        int result= a/b;
        System.out.println("#####"); //产生异常后,异常后面的语句不再执行
        return result;
    }
}

在这里插入图片描述

数值转换异常:
在这里插入图片描述
throw
throw:用在方法中。由用户产生异常类对象,而非JVM产生,一般与自定义异常搭配使用。
下面代码自定义异常:
比如在微信红包中,一次最多是200元,超过200会出已异常:

/////throw
class MyException extends Exception
{
      public MyException(String str)
      {
          super(str);
      }
}
public class Throw1
{
    public static void main(String[] args) throws Exception  //将异常抛给JVM
    {
        try
        {
            int num=300;
            if(num>200)
              throw new MyException("微信红包至多200元");
        }
        catch (MyException e)
        {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述
面试题:请解释throw和throws的区别

1. throw用于方法内部,主要表示手工异常抛出。
2. throws主要在方法声明上使用,明确告诉用户本方法可能产生的异常,同时该方法可能不处理此异常。

现在要求编写一个方法进行除法操作,但是对于此方法有如下要求:

  1. 在进行除法计算操作之前打印一行语句"****".
  2. 如果在除法计算过程中出现错误,则应该将异常返回给调用处。
  3. 不管最终是否有异常产生,都要求打印一行计算结果信息。
    在方法内抛异常,用在产生异常处处理异常将异常抛出:
public class Throw1
{
    public static void main(String[] args)
    {
        try
        {
            System.out.println(cual(10,0));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static int cual(int a,int b) 
    {
        int result=0;
        System.out.println("******");
        try {
            result=a/b;
        }catch (Exception e)
        {
            throw  e;
        }finally {
            System.out.println(result);
        }
        return result;
    }
}

直接在方法上throws

public static int cual(int a,int b) throws Exception
    {
        int result=0;
        System.out.println("******");
        try {
            result=a/b;
        }finally {
            System.out.println(result);
        }
        return result;
  }

assert
语法:
assert 布尔表达式:“返回false执行的代码块”
当断言返回false时会抛出断言异常。异常后面语句不会执行。
java 断言开启参数(JVM参数)为 -ea,默认 断言关闭
[run]–>[Edit Configurations]—>[VM options] —ea。

public class Throw1
{
    public static void main(String[] args)
    {
        int num=10;
        assert num==55 :"num应该为55";
        System.out.println(num);
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sophia__yu/article/details/83659538