异常处理
来源:互联网 发布:游戏帧数限制软件 编辑:程序博客网 时间:2024/06/01 09:03
public class ArrayAverage {public static void main(String rgas[]){//int a[]={0,0,0,0};int a[]=toIntArray(rgas);double avg=average(a);System.out.println(avg);System.out.println("Over!");}/*以后开发,凡是碰到引用变量,要访问里面堆内存中 的数据,都要用"判断引用是否为null的卫条件"去防护一下! 否则就有空指针异常的bug! */ public static int[] toIntArray( String strs[] ){ // 10 aq 20 30 rw12 30 int[] ar=new int[strs.length]; int i=0; int count=0;//有效整数的个数 while(i<ar.length){ try{ ar[count] = Integer.parseInt(strs[i]);//aa count++; }catch(NumberFormatException e){ //e.printStackTrace(); System.out.println("遇到非法数据:"+strs[i]+",不能转换成整数!"); }catch(Exception e){ e.printStackTrace(); }finally{ i++; } } //把ar[]中的有效元素提取成一个新的数组---前count个有效 int[] a=new int[count]; System.arraycopy(ar,0,a,0,count); return a; } public static double average(int[] a){ if(a==null || a.length==0){//卫条件 return 0; } double sum = 0; for(int i=0;i<a.length;i++){ sum +=a[i]; } return sum/a.length; } }
第二种情况:public class MyDate {private int year;private int month;private int day;public MyDate(int year,int month ,int day)throws MyDateException{//防护{set(year,month,day);}public void set(int year,int month,int day)throws MyDateException{//防护//if(year<0){//throw new MyDateException("错误的年份:"+year);//}elseif(month<0 || month>12){//System.out.println("月份错误");//return;throw new MyDateException("错误的月份:"+month);}//else{//if(day<0||day>31){//throw new MyDateException("错误的天数:"+day);//}this.year = year;this.month =month;this.day = day;}public String toString(){return year+"年"+month+"月"+day+"日";}//如果我们代码中出现RuntimeException或者它的子类异常,当我们不处理时,编译(javac)是能够通过的,但运行时若出现则会挂。 //如果存在非运行时异常(不是RuntimeException或者它的子类异常),则必须进行处理(抛或try-catch),否则编译通不过! public void aa(){ int i = Integer.parseInt("1ww2");//运行时异常,不处理,编译能通过 System.out.println("i="+i); }}
public class MyDateException extends Exception {public MyDateException(){}public MyDateException(String mgs){super(mgs);}}
import java.util.Scanner;public class Client {private static Scanner sc = new Scanner(System.in);public static void main(String[] args) {login();}public static void login(){System.out.println("请输入日期(格式如2017/3/31):");String str = sc.nextLine();String strs[] =str.split("/");if(strs.length!=3){System.out.println("日期格式错误,请重新输入...");login();}try{int year = Integer.parseInt(strs[0]);int month = Integer.parseInt(strs[1]);int day = Integer.parseInt(strs[2]);MyDate d1 = new MyDate(year, month, day);System.out.println(d1);}catch(NumberFormatException e){System.out.println("日期格式错误"+e.getMessage()+",请重新输入..."); login();}catch(MyDateException e){System.out.println( e.getMessage());login();}catch(Exception e){System.out.println(e.getMessage());login();}}}
1 0
- 异常处理+异常+处理异常+自定义异常
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 异常处理
- 继承——java面向对象
- static静态
- Caffe源码解读(十二):自定义数据输入层
- js之 同一页面中的多表单提交
- Android-ABIFilter-Device supports x86,but APK only supports armeabi-v7a,armeabi,x86_64
- 异常处理
- 计算机解决问题的方法
- Java源码——对象序列化(对象的存储及读取)(Object Serialization)
- HDU 1753
- lucene、solr的介绍及区别
- Java Socket实战之一 单线程通信
- UNIX高级环境编程学习(三)----------------基础知识
- Android内存泄露
- ADS-B显示终端7.0