博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java异常处理
阅读量:4545 次
发布时间:2019-06-08

本文共 3869 字,大约阅读时间需要 12 分钟。

 

 

这是课程里给的代码,自己看着讲义在里面加了些注释。

import java.io.*;class TryWithResourcesTest {	// 可以抛出子类异常(更具体的异常),但不能抛出更一般的异常    public static void main(String ... args)		throws IOException				{		String path = "c:\\aaa.txt";		System.out.println( ReadOneLine1( path ) );		System.out.println( ReadOneLine2( path ) );    }	static String ReadOneLine1(String path){		BufferedReader br=null;        try {            br=new BufferedReader(new FileReader(path));            return br.readLine();        } catch(IOException e) {            e.printStackTrace();        } finally {            if(br!=null){				try{ 					br.close();				}catch(IOException ex){				}			}        }		return null;	}	static String ReadOneLine2(String path)		throws IOException	{		try(BufferedReader br= new BufferedReader(new FileReader(path))){            return br.readLine();        }	}}

  

public class TestTryFinally {    public static String output = "";    public static void foo(int i) {        try {            if (i == 1) {                throw new Exception();            }            output += "1";        } catch(Exception e) {            output += "2";            return;        } finally {            output += "3";  // 不论是否出现异常都会执行。        }        output += "4";    }    public static void main(String args[]) {        foo(0);        System.out.print(output + " ");        // foo(1);        // System.out.println(output);    }}

  

import java.io.*;public class ExceptionTrowsToOther{	public static void main(String[] args){			try{			System.out.println("====Before====");		 	readFile();			System.out.println("====After====");		 }catch(IOException e){ System.out.println(e); }	}	/**	 * 受检的异常:	 * 要么捕(catch)	 * 要么抛(throws): 在方法的签名后面用throws ***来声明	 * 		在子类中, 如果要覆盖父类的一个方法, 若父类的方法中方法声明了throws异常,则子类的方法也可以throws异常	 * 		可以抛出子类异常(更具体的异常),但不能抛出更一般的异常	 */	public static void readFile()throws IOException {		FileInputStream in=new FileInputStream("myfile.txt");		int b;			b = in.read();		while(b!= -1)   {			System.out.print((char)b);			b = in.read();		}		in.close();		}}

  

import java.io.*;public class ExceptionForNum {	public static void main(String[] args) 	{		try{			BufferedReader in = new BufferedReader(				new InputStreamReader( System.in ) );			System.out.print("Please input a number: ");			String s = in.readLine();			int n = Integer.parseInt( s );		}catch(IOException ex){			ex.printStackTrace();		}catch(NumberFormatException ex){			ex.printStackTrace();		}	}}

  

public class ExceptionCause {	/**	 * 基本写法:	 * try {	 * 		语句组;	 * } catch (Exception ex) {	 * 		异常处理语句组;	 * }	 * 	 */	public static void main(String [] args)	{		try 		{			BankATM.GetBalanceInfo( 12345L);		}catch(Exception e)	{	//	捕获异常代码			System.out.println("something wrong " + e);			System.out.println("cause" + e.getCause());		}	}}/** * 对于异常,不仅要进行捕获处理, 有时还需要将此异常进一步传递给调用者,  * 以便让调用者也能感受到这种异常。 *  * 1. 将当前捕获的异常再次抛出: * 		throw e; * 2. 重新生成一个新的异常并抛出: * 		throw new Exception("some message"); * 3. 重新生成并抛出一个新的异常, 该异常中包含了当前异常的信息: * 		throw new Exception("some message", e); * 		可用getCause()来得到内部异常 */class DataHouse {	public static void FindData( long ID)		throws DataHouseException	{		if( ID>0 && ID<1000)			System.out.println( "id: " + ID );		else			throw new DataHouseException("cannot find the id");	}}class BankATM{	public static void GetBalanceInfo( long  ID)		throws MyAppException	{		try 		{			DataHouse.FindData(ID);		}catch (DataHouseException e) {			throw new MyAppException("invalid id",e);		}	}}/** * 创建用户自定义异常类时: * 1. 继承自Exception类或某个子Exception类 * 2. 定义属性和方法, 或重载父类的方法 */class DataHouseException extends Exception {	public DataHouseException( String message ) {		super(message);	}}class MyAppException extends Exception {	public MyAppException (String message){ 		super (message); 	}	public MyAppException (String message, Exception cause) {		super(message,cause);	}   }

  

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/10515661.html

你可能感兴趣的文章
多个tomcat实例运行的配置
查看>>
一种基于 Numpy 的 TF-IDF 实现报告
查看>>
wpf窗口阴影
查看>>
linux内核分析第四周-使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用...
查看>>
Centos 7升级内核
查看>>
Pandas 基本技巧
查看>>
hdu 1264
查看>>
hdu 1273不会的题
查看>>
(转)父子窗体的菜单合并及工具栏合并
查看>>
分页SQL
查看>>
linux系统使用sh文件传参数给matlab程序
查看>>
软工实践原型设计-黄紫仪
查看>>
食用指南
查看>>
CSS3圆角详解(border-radius)
查看>>
Python正则表达式指南
查看>>
前端学习之JavaScript中的 NaN 与 isNaN
查看>>
chrome安装json view插件
查看>>
CSS div 高度满屏
查看>>
页面回发速度由 6 秒减少为 0.6 秒的真实案例!
查看>>
一种实现C++反射功能的想法(一)
查看>>