JavaSE05

14 篇文章 0 订阅
package day05;
/**
 * Java异常捕获异常机制中
 * try-catch
 * 语法:
 * try{
 *     代码片段
 *  } catch(异常类){
 *  捕获try代码片段中的XXException并处理
 *  }
 */
public class DemoTryCatch_1 {
	public static void main(String[] args) {
		try{
			//String str = null;
			String str2 = "";
			//System.out.println(str.length());
			//System.out.println(Integer.parseInt(str));
			System.out.println(str2.charAt(1));
		} catch(NumberFormatException e){
			//e.printStackTrace();	//日志跟踪
			System.out.println("数据转换异常!");
		} catch (NullPointerException e) {				//子类异常
			System.out.println("空指针异常!");
			/*
			 * 应当养成一个好习惯,最后一个catch捕获Exception放置因为没有
			 * 捕捉到的异常导致程序抛出异常是从小到大(从子类异常到父类异常)
			 */
		} catch(Exception e){							//父类异常
			System.out.println("反正就是出了个错!");
		}
		
		
	}
}

package day05;
/**
 * 常见RunTimeException的5种异常:
 * 1、制造空指针异常
 * NullPointerException
 * 
 * 2、制造数组越界异常
 * ArrayIndexOutOfBoundsException
 * 
 * 3、制造数学异常
 * ArithmeticException
 * 
 * 4、强制类型转换异常
 * ClassCastException
 * 
 * 5、制造数值格式化异常
 * NumberFormatException
 */
public class DemoTryCath02_2 {
	public static void main(String[] args) {
		//NullPointerException  空指针异常
//		String str = null;
//		System.out.println(str.length());	
		
		//ArrayIndexOutOfBoundsException  下标越界
//		int[] arr = new int[2];
//		System.out.println(arr[2]);
		
		//ArithmeticException   数学异常
		/*double a = 5/0;
		System.out.println(a);*/
		
		//ClassCastException   强制类型转换异常
		Object obj = "hello";
		System.out.println((Integer)obj);
		
		//NumberFormatException  数值格式化异常
		String str = "s";
		System.out.println(Integer.parseInt(str));
	}
}

package day05;
/**
 * finally块:
 * finally块只能定义在异常处理机制的最后,可以直接跟在try后面或者最后一个
 * catch之后,finally可以保证只要程序运行到try当中,那么无论try当中的代码片段
 * 是否出现异常,finally块里面的代码都必须执行;
 * 
 * 通常把释放资源等操作放在finally中,比如:流的关闭;
 * 
 * 注意:finally是在return之前执行的
 *
 */
public class DemoFinally_3 {
	public static void main(String[] args) {
		System.out.println("程序开始执行:");
		try {
			String str = "shg";
			System.out.println(str.charAt(2));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("finally代码执行了:");
		}
	}
}

package day05;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 操作流中的异常捕获及其释放流资源
 */
public class FinallyDemo2_4 {
	public static void main(String[] args) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("osw.txt");
			byte[] bytes = new byte[100];
			int len = fis.read(bytes);
			System.out.println(new String(bytes,0,len,"UTF-8"));
		} catch (Exception e) {
			System.out.println("读取文件出现异常...");
			e.printStackTrace();
		} finally{
			try {
				if(fis!=null){
					System.out.println("关闭流,释放资源");
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

package day05;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/*
 * throws:
 * 在方法上抛出异常,要求调用方来解决异常
 * 注意:throws不要在主程序main方法上抛出
 * 
 * 调用方抛出的异常必须大于或等于被调用方抛出的异常
 * 
 */
public class DemoThrows_5 {
	public static void testRead() throws FileNotFoundException{
		FileInputStream fis = new FileInputStream("pw2.txt");
	}
	
	public static void test() throws FileNotFoundException{
		testRead();
	}
	
	public static void main(String[] args) {
		try {
			test();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

package day05;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * JDK1.7之后,推出一个新特性,自动关闭
 */
public class DemoTryCatch_6 {
	public static void main(String[] args) {
		/*
		 * AtuoCloseable接口的子类还可以定义在这里
		 * 自动关闭语法是编译器认可,编译后的class文件中流的关闭还是finally中进行
		 */
		try(PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("ceshi.txt"))));) {
			pw.println("祝愿全天下的母亲节日快乐!");
			System.out.println("写入完毕!");
		} catch (Exception e) {
			e.printStackTrace();//日志跟踪
		}
		/*
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("pw.txt");
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		
		
	}
}

package day05;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * throw:
 * 可将RuntimeException异常抛给客户端
 * @author 臻冉
 *
 */
public class DemoThrow_7 {
	public static void main(String[] args) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("hhh.txt");
		} catch (Exception e) {
			//e.printStackTrace();//日志跟踪
			/*
			 * 一旦执行,程序不再往后执行
			 */
			throw new RuntimeException("读取失败,请联系相关人员...");		//类似于加了一个return
		} finally {
			try {
				if(fis != null){
					fis.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("程序结束!");
	}
}

package day05;

import java.util.Scanner;

/**
 * throw
 * kejiang RuntimeException异常抛给客户端,
 * 前提是需要我们异常类来捕获RuntimeException的异常信息
 * @author 臻冉
 *
 */
public class demoRuntimeException_8 {
	
	public static void register(){
		String name = "admin";
		Scanner scanner = new Scanner(System.in);
		String inputName = scanner.next();
		if(name.equals(inputName)){
			throw new RuntimeException("用户名被占用");
		} else {
			System.out.println("请继续填写:");
		}
	}
	
	public static void main(String[] args) {
		try {
			register();
		} catch (RuntimeException e) {
			String excMessage = e.getMessage();//捕获异常信息
			System.out.println(excMessage);
		}
	}
}

package day05;
/**
 * 自定义异常
 * 必须要继承父类RuntimeException
 * @author 臻冉
 *
 */
public class DemoRuntimeException02_9 {
	public static void register(){
		String name = "admin";
		if(name.equals("admin")){
			throw new NameAlreadyException("姓名被占用!");
		}
	}
	
	public static void main(String[] args) {
		try {
			register();
		} catch (RuntimeException e) {
			System.out.println(e.getMessage());		//获取异常信息
		}
		
	}
}

package day05;
/**
 * 使用当前测试异常的抛出
 * @author 臻冉
 *
 */
public class Person_11 {
	
	private int age;

	public int getAge() {
		return age;
	}

	public void setAge(int age) throws AgeException {
		/*
		 * 一个方法内部使用throw抛出一个异常,就要在方法上使用throws
		 * 声明噶异常的抛出已告知调用方处理这个异常
		 * 只有RuntimeException及其子类型异常在方法中阿婆出是不需要在方法上
		 * 声明该异常的抛出,其他类型异常则必须在方法后使用throws抛出异常
		 * 否则编译不通过
		 */
		
		if(age<0 || age>120){
			throw new AgeException("年龄不合法,请认真填写您的年龄!");
		}
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person_11 [age=" + age + "]";
	}
}

package day05;
/**
 * 异常的抛出:
 * throw关键字,用于将一个异常抛出
 * 通常两种情况会主动抛出一个异常
 * 	1、程序遇到一个满足语法要求,但是不满足业务逻辑要求的时候可主动抛出一个异常给调用者
 * 
 * 	2、程序出现了异常,但是不应当在当前代码片段中解决,该异常可以抛出给调用者
 * 
 * 当我们调用一个含有throws声明异常抛出的方法时,编译器要求我们必须处理这个异常,
 * 处理方式有两种:
 * 1、try-catch自己捕获异常并处理
 * 2、在当前方法上继续使用throws声明,将该异常抛出,那么调用者需要来处理这个相关异常
 * 
 */

public class DemoException02_12 {
	public static void main(String[] args) {
		Person_11 p = new Person_11();
		try {
			p.setAge(10000);
		} catch (AgeException e) {
			System.out.println(e.getMessage());
		}
	}
}

package day05;
/**派生类所抛出的异常小于或等于超类的异常*/
public class DemoException03_13 {
	public void test() throws Exception{
		
	}
}


class Demo04 extends DemoException03_13{
	//重写父类方法
	public void test() throws NullPointerException{
		
	}
}
package day05;

import java.awt.AWTException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

/**子类重写父类含有throws声明异常抛出的方法时对throws的重写原则*/
public class DemoException04_14 {
	public void dosome() throws IOException,AWTException{
		
	}
}


class Son extends DemoException04_14{ 
	//可以不再抛出任何异常
	/*public void dosome(){
		
	}*/
	
	//可以抛出部分异常
//	public void dosome() throws IOException{
//		
//	}
	
	//可以抛出父类方法中抛出异常的子类型异常
//	public void dosome() throws FileNotFoundException{
//		
//	}
	
//	不允许抛出额外异常
//	public void dosome() throws SQLException{
//		
//	}
	
	//不可以抛出父类方法抛出异常的父类型异常
//	public void dosome() throws Exception{
//		
//	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值