制造异常对象:throw

/*
一、制造异常:throw
throw 使用在方法体内,后面跟异常对象
 */
public class ThrowTest {
    public static void main(String[] args) {
        try {
            div(10,0);
        }catch (ArithmeticException e){
            e.printStackTrace();
        }
    }
    public static int div(int a,int b){
        return a/b;
    }
}

 

/*
一、制造异常:throw
throw 使用在方法体内,后面跟异常对象
 */
public class ThrowTest {
    public static void main(String[] args) {
        try {
            div(10,0);
        }catch (ArithmeticException e){
            e.printStackTrace();
        }
    }
    public static int div(int a,int b){
        if (b==0){
            throw new ArithmeticException("除数不能为零!!!");
        }
        else {
            return a/b;
        }
    }
}

 

throw【可以代替return】:

 throw与自定义异常:

二、自定义异常类:
①声明一个类继承一个异常类
(若继承的是RuntimeException,则该异常为运行时异常,若继承的是Exception,则该异常为编译时异常)

/*
自定义异常
 */
public class MyException  extends RuntimeException{
    public MyException(){

    }
    public MyException(String message){
        super(message);
    }
}
/*
一、制造异常:throw【可以代替return】
throw 使用在方法体内,后面跟异常对象
面试题:throws与throw的区别?
二、自定义异常类:
①声明一个类继承一个异常类
(若继承的是RuntimeException,则该异常为运行时异常,若继承的是Exception,则该异常为编译时异常)
 */
public class ThrowTest {
    public static void main(String[] args) {
        try {
            div(10,0);
        }catch (MyException e){
            e.printStackTrace();
            String msg= e.getMessage();
            System.out.println(msg);
        }
    }
    public static int div(int a,int b){
        if (b==0){
            //throw new ArithmeticException("除数不能为零!!!");
            throw  new MyException("除数不能为零!!!");
        }
        else {
            return a / b;
        }
    }
}

 运行时异常:

public class TestException {
    public static void main(String[] args) {

        try {
            login("use","123456");
        }catch (RuntimeException e){
            String msg= e.getMessage();
            System.out.println(msg);
        }

    }
    //需求:模拟登录功能
    public static void login(String username,String password){
        if( "user".equals(username) && "123456".equals(password)){
            System.out.println("登录成功!");
        }else{
            throw new RuntimeException("用户名或密码错误!");
        }
    }

}

选择编译时异常 :

package UserNamePassword;

public class TestException {
    public static void main(String[] args) {

//        try {
//            login("use","123456");
//        }catch (RuntimeException e){
//            String msg= e.getMessage();
//            System.out.println(msg);
//        }


        login("use","123456");

    }
//    //需求:模拟登录功能
//    public static void login(String username,String password){
//        if( "user".equals(username) && "123456".equals(password)){
//            System.out.println("登录成功!");
//        }else{
//            throw new RuntimeException("用户名或密码错误!");
//        }
//    }
    //需求:模拟登录功能
    public static void login(String username,String password)throws Exception{
            if( "user".equals(username) && "123456".equals(password)){
            System.out.println("登录成功!");
        }else{
            throw new Exception("用户名或密码错误!");//编译时异常
        }
}

}

 

 

package UserNamePassword;

public class TestException {
    public static void main(String[] args) {

//        try {
//            login("use","123456");
//        }catch (RuntimeException e){
//            String msg= e.getMessage();
//            System.out.println(msg);
//        }


        login("use","123456");

    }
//    //需求:模拟登录功能
//    public static void login(String username,String password){
//        if( "user".equals(username) && "123456".equals(password)){
//            System.out.println("登录成功!");
//        }else{
//            throw new RuntimeException("用户名或密码错误!");
//        }
//    }
    //需求:模拟登录功能
   /* public static void login(String username,String password)throws Exception{
            if( "user".equals(username) && "123456".equals(password)){
            System.out.println("登录成功!");
        }else{
            throw new Exception("用户名或密码错误!");//编译时异常
        }
    }*/
        //需求:模拟登录功能
        public static void login(String username,String password)throws UserNamePasswordErrorException{
            if( "user".equals(username) && "123456".equals(password)){
             System.out.println("登录成功!");
            }else{
                throw new UserNamePasswordErrorException("用户名或密码错误!");
            }
        }

}

 

package UserNamePassword;

public class TestException {
    public static void main(String[] args) {
        try {
            login("use","123456");
        } catch (UserNamePasswordErrorException e) {
            //e.printStackTrace();
            String msg= e.getMessage();
            System.out.println(msg);
        }

    }
        //需求:模拟登录功能
        public static void login(String username,String password)throws UserNamePasswordErrorException{
            if( "user".equals(username) && "123456".equals(password)){
             System.out.println("登录成功!");
            }else{
                throw new UserNamePasswordErrorException("用户名或密码错误!");
            }
        }

}

 Exer:

public class Exer {
    public static void main(String[] args) {
        try {
            methodA();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        methodB();
    }
    static void methodA() {
        try{
        System.out.println("进入方法A");
        throw new RuntimeException("制造异常");
    }finally {
        System.out.println("用A方法的finally" );
    }
}
    static void methodB() {
try {
        System.out.println("进入方法B");
        return;
        }finally {
        System.out.println("调用B方法的finally" );
            }
                    }
}

Exer2:

编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除。
对数据类型不一致(NumberFormatException)、
缺少命令行参数(ArrayIndexOutOfBoundsException)、除O(ArithmeticException)
输入负数(EcDef自定义的异常)进行异常处理。
提示:
(1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能。
(2)在main()方法中使用异常处理语句进行异常处理。
(3)在程序中,自定义对应输入负数的异常类(EcDef)。
(4)运行时接受参数java EcmDef 20 10
//args[0]="20"args[1]="10”
(5)nterger类的static方法parselInt(String s)将s转换成对应的int值。如int a=Interger.parselnt("314");//a=314;

 


/*
编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除。
对数据类型不一致(NumberFormatException)、
缺少命令行参数(ArrayIndexOutOfBoundsException)、除O(ArithmeticException)
输入负数(EcDef自定义的异常)进行异常处理。
提示:
(1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能。
(2)在main()方法中使用异常处理语句进行异常处理。
(3)在程序中,自定义对应输入负数的异常类(EcDef)。
(4)运行时接受参数java EcmDef 20 10
//args[0]="20"args[1]="10”
(5)nterger类的static方法parselInt(String s)将s转换成对应的int值。如int a=Interger.parselnt("314");//a=314;

 */

public class EcmDef {
    public static void main(String[] args) {
        try {
            int num1=Integer.parseInt(args[0]);
            int num2=Integer.parseInt(args[1]);
            ecm(num1,num2);
        }catch (ArithmeticException e){
            e.printStackTrace();
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
        }catch (NumberFormatException e){
            e.printStackTrace();
        }catch (EcDef ecDef){
            ecDef.printStackTrace();
            System.out.println(ecDef.getMessage());
        }
        System.out.println("庞大的功能");

    }
    public static int ecm(int a,int b){
            if (a<0||b<0){
                throw new EcDef("不能为负数!");
            }
            else {
                return a/b;
            }
    }
}

 

public class EcDef extends RuntimeException{
    public EcDef(){

    }
    public EcDef(String msg){
        super(msg);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值