码迷,mamicode.com
首页 > 其他好文 > 详细

异常类

时间:2016-10-08 23:29:45      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

        int a=0,b=0,c=0;
        try{
            a=Integer.parseInt("888");
            b=Integer.parseInt("as888");//这个try在这中断
            c=88;
        }
        catch(NumberFormatException e){
            System.out.println("发生异常 "+e.getMessage());
        }
        System.out.printf("%d %d %d",a,b,c);
        try{
            throw new java.io.IOException("fuck");
        }
        catch(java.io.IOException e){
            System.out.println(e.getMessage());
        }
import java.util.Scanner;
public class test{
    public static void main(String args[]){
        Bank bank=new Bank();
        try{
            bank.income(111, 32);
            bank.income(23323,324);
            bank.income(2323, 34);
            bank.income(2, -34234);
            bank.income(23, 2);
        }
        catch(BankException a){
            System.out.println(a.readMessage());    
        }
    }
}

class BankException extends Exception{
    String message;
    public BankException(int a,int b){
            message="出错了日你";
    }
     String readMessage(){
        return message;
    }
}

class Bank{
    int money;
    void income(int a,int b)throws BankException{//在这里就要声明要产生的异常
        if(a+b<0){
            throw new BankException(a,b);//然后写出具体的异常操作
        }
        money=a+b;
        System.out.println("input ..."+money);
    }
}

断言

aasert

assert 表达式:一条string

int []a={1,-2,3,-4,3};
        for(int i:a){
            assert i>0:"不能小于0";
            System.out.println(i);
        }

 

异常类

标签:

原文地址:http://www.cnblogs.com/vhyc/p/5940204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!