throws抛异常

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hju22/article/details/83352698

如果调用的代码有异常抛出,那需要try…catch处理或者抛异常

package com.exception.test;

import java.util.Scanner;

public class Throws {
    public static void main(String[] args) {
        Calculate cal=new Calculate();
        try {
            cal.remainder();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("程序继续执行");

    }
}
class Calculate{
    void remainder() throws Exception{
        int a,b,c;
        Scanner sc=new Scanner(System.in);
        a=sc.nextInt();
        b=sc.nextInt();
        c=a%b;
        System.out.println("余数为:"+c);

    }
}

结果:
坚持比努力更重要

猜你喜欢

转载自blog.csdn.net/hju22/article/details/83352698