栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

制造异常对象:throw

Java 更新时间:发布时间: 百科书网 趣学号
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;
    }
}

 

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);
    }
}
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 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;

 



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);
    }
}

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1073493.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号