Java异常处理
这是课程里给的代码,自己看着讲义在里面加了些注释。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 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); } } |
__EOF__
![](https://files.cnblogs.com/files/ruruozhenhao/mind-bending-gifs-by-florian-de-looij-1.gif)
本文作者:Veritas des Liberty
本文链接:https://www.cnblogs.com/h-hkai/p/10515661.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/h-hkai/p/10515661.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
永远渴望,大智若愚(stay hungry, stay foolish)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
2018-03-12 B - Factors of Factorial