• Java处理异常小试


    /*1.尝试读取一个文件:
    
    这里的读取和写入直接放在try块,这样出了catch块引用自动消失,指向的对象可以被系统回收,catch块什么也不用做,这是JDK1.7之后常用的写法。(Orz其实自己也不是很懂,问了tao哥这里tao哥扩展讲的)*/
    
    package com.zmz.exception;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    
    public class CheckedException {
    	public static void main(String[] args) {
    		
    		
    		try (FileReader reader = new FileReader("");
    				FileWriter writer = new FileWriter("")) {
    			
    			int c = reader.read();
    			System.out.println(c);
    			
    //			reader.close();
    //			writer.close();
    			
    		} catch (IOException e) {
    		}
    		
    		
    		System.out.println("start");
    		FileReader reader = null ;
    		
    		try {
    			reader = new FileReader("abc");
    			int c= reader.read();
    			System.out.println((char) c);
    		} catch (FileNotFoundException e) {
    			//文件不存在
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			//在读取的过程中可能出现读取数据或者写入数据出错了(比如中途硬盘坏了)
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally {
    //				if(reader != null ) {
    //					try {
    //						reader.close();
    //					} catch (IOException e) {
    //						reader = null;
    //					}
    //					System.out.println("释放资源");
    //				}
    		
    		}
    		
    	}
    }
    
    /*2.为员工设置年龄,对超出范围的处理异常:*/
    package com.zmz.exception;
    
    public class Staff {
    	private String name;
    	private int age;
    	
    	public Staff(String name) {
    		this.name = name;
    		age = 18;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	/**
    	 * 修改员工年龄
    	 * @param age 
    	 * 				int 年龄大于18,小于60
    	 * @throws TooYoungException
    	 * @throws TooOldException
    	 */
    	public void setAge(int age) throws TooYoungException, TooOldException {
    		if(age < 18) {
    			//太小
    			//中断了程序的正常流程,向上抛出(传递)一个自定义信号
    			throw new TooYoungException(age);
    		}
    		if(age > 60) {
    			//太大
    			throw new TooOldException(age);
    		}
    		this.age = age;
    	}
    	
    	
    }
    
    package com.zmz.exception;
    
    public class TooOldException extends Exception{
    	public TooOldException(int age) {
    		// TODO Auto-generated constructor stub
    		super("年龄太大" + age);
    	}
    }
    
    package com.zmz.exception;
    
    public class TooYoungException extends Exception {
    	private static final long serialVersionUID = 1L;
    	public TooYoungException(int age) {
    		super("年龄太小"+age);
    	}
    
    }
    package com.zmz.exception;
    
    import java.util.*;
    
    public class Test {
    			
    		//编译型错误,强制要处理
    	public static void main(String[] args) {
    		Staff s1 = new Staff("Bob");
    		System.out.println(s1.getName());
    		System.out.println(s1.getAge());
    		
    		try {
    			s1.setAge(12);
    			System.out.println(s1.getAge());
    			
    		} catch (TooYoungException e) {
    			// TODO Auto-generated catch block
    			resetAge(s1, e.getMessage());
    //			e.printStackTrace();
    		} catch (TooOldException e) {
    			// TODO Auto-generated catch block
    			resetAge(s1, e.getMessage());
    //			e.printStackTrace();
    		}
    	}
    		
    		//handleXxxExpception
    		private static void resetAge(Staff s1,String message) {
    			System.out.println(message);
    			Scanner sc = new Scanner(System.in); 
    			System.out.println("请输入年龄");
    			int age = sc.nextInt();
    			try {
    				s1.setAge(age);
    			} catch (TooYoungException e) {
    				// TODO: handle exception
    				resetAge(s1, e.getMessage());
    			}catch (TooOldException e2) {
    				// TODO: handle exception
    				resetAge(s1, e2.getMessage());
    			}	
    		}
    			
    	}
    	
    	
    
    
    /*3.用户信息的更新(常用设计模式):
    待续。。。*/
    
    

  • 相关阅读:
    关于课内外读物的建议
    c# Aes加解密
    web api 如何通过接收文件流的方式,接收客户端及前端上传的文件
    c# 文件夹权限
    mysql 8创建远程访问用户以及连接mysql速度慢的解决方法
    为什么读书?读书让我们明心见性!
    大部分教程不会告诉你的 12 个 JS 技巧
    nuget包管理nuget服务器发布包时出现请求报错 406 (Not Acceptable)
    Python 实现毫秒级淘宝、京东、天猫等秒杀抢购脚本
    eos的资源和工具列表
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256605.html
Copyright © 2020-2023  润新知