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); }
时间: 2024-10-23 16:31:02