首页 / JAVA / java开始到熟悉72-76
java开始到熟悉72-76
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java开始到熟悉72-76,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6730字,纯文字阅读大概需要10分钟。
内容图文
![java开始到熟悉72-76](/upload/InfoBanner/zyjiaocheng/1182/d0e6868f6a564c05b3e33eb0542f044b.jpg)
本次内容:异常机制
1、为什么需要异常
2、异常
3、error类
4、exception类
5、exception类中的unchecked exception
举例:
6、常用异常处理方法
a.try
注意:一个try语句块至少得带一个finally语句块或catch语句块
1 package array; 2 /** 3 * exception 4 * @author acer 5 * 6 */ 7 public class exception { 8 public static void main(String[] args) 9 { 10 try { 11 Thread.sleep(1000); 12 } catch (InterruptedException e) { 13// TODO Auto-generated catch block14 e.printStackTrace(); 15 } 16 } 17 }
1 package array; 2 /** 3 * try catch finally 4 */ 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.IOException; 8 9 public class trycatch { 10 public static void main(String[] args) 11 { 12 FileReader f=null; 13try { 14 f=new FileReader("d:/a.txt"); 15char c,d; 16 c = (char)f.read(); 17 d = (char)f.read(); 18 System.out.println(""+c+d); 19 } catch (FileNotFoundException e) { 20 e.printStackTrace(); 21 } catch (IOException e) { 22 e.printStackTrace(); 23 }finally{ 24try { 25if(f!=null) 26 f.close(); 27 } catch (IOException e) { 28 e.printStackTrace(); 29 } 30 } 31 } 32 }
运行结果:
ab
(说明:a.txt文本中的内容:abcdefg)
不同内容:try,catch,finally,return执行顺序
代码1:
1 package array; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 7 /** 8 * 测试try,catch,finally,return执行顺序 9 * @author acer 10 * 11 */ 12 public class exeshunxu { 13 public static void main(String[] args) 14 { 15 String str=new exeshunxu().openfile(); 16 System.out.println(str); 17 } 18 String openfile() 19 { 20try { 21 System.out.println("aaa"); 22 FileInputStream fi=new FileInputStream("d:/a.txt"); 23int a=fi.read(); 24 System.out.println("bbb"); 25return "try"; 2627 } catch (FileNotFoundException e) { 28 System.out.println("catchingchild.................."); 29 e.printStackTrace(); 30return "catch filenotfoundexception"; 31 } catch (IOException e) { 32 System.out.println("catchingfather................"); 33 e.printStackTrace(); 34return "catch ioexception"; 35 }finally{ 36 System.out.println("finally..............."); 37return "finally"; 38 } 39 } 40} 41
运行结果:
aaa
bbb
finally...............
finally
代码2:
1 package array; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 7 /** 8 * 测试try,catch,finally,return执行顺序 9 * @author acer 10 * 11 */ 12 public class exeshunxu { 13 public static void main(String[] args) 14 { 15 String str=new exeshunxu().openfile(); 16 System.out.println(str); 17 } 18 String openfile() 19 { 20try { 21 System.out.println("aaa"); 22 FileInputStream fi=new FileInputStream("d:/a.txt"); 23int a=fi.read(); 24 System.out.println("bbb"); 25return "try"; 2627 } catch (FileNotFoundException e) { 28 System.out.println("catchingchild.................."); 29 e.printStackTrace(); 30return "catch filenotfoundexception"; 31 } catch (IOException e) { 32 System.out.println("catchingfather................"); 33 e.printStackTrace(); 34return "catch ioexception"; 35 }finally{ 36 System.out.println("finally..............."); 37 } 38 } 39} 40
运行结果:
aaa
bbb
finally...............
try
代码3:
1 package array; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 7 /** 8 * 测试try,catch,finally,return执行顺序 9 * @author acer 10 * 11 */ 12 public class exeshunxu { 13 public static void main(String[] args) 14 { 15 String str=new exeshunxu().openfile(); 16 System.out.println(str); 17 } 18 String openfile() 19 { 20try { 21 System.out.println("aaa"); 22 FileInputStream fi=new FileInputStream("d:/abcd.txt"); 23int a=fi.read(); 24 System.out.println("bbb"); 25return "try"; 2627 } catch (FileNotFoundException e) { 28 System.out.println("catchingchild.................."); 29 e.printStackTrace(); 30return "catch filenotfoundexception"; 31 } catch (IOException e) { 32 System.out.println("catchingfather................"); 33 e.printStackTrace(); 34return "catch ioexception"; 35 }finally{ 36 System.out.println("finally..............."); 37 } 38 } 39} 40
运行结果:
aaa
catchingchild..................
java.io.FileNotFoundException:
d:\abcd.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native
Method)
at
java.io.FileInputStream.<init>(FileInputStream.java:138)
at
java.io.FileInputStream.<init>(FileInputStream.java:97)
at
array.exeshunxu.openfile(exeshunxu.java:22)
at
array.exeshunxu.main(exeshunxu.java:15)
finally...............
catch
filenotfoundexception
(注释:D盘中有a.txt文件,但没有abcd.txt文件)
由此可得到执行顺序为:
>1、执行try,catch,给返回值赋值;
>2、执行finally;
>3、return;
(一般不要在finally中使用return语句)
7.
1 package array; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileReader; 5 import java.io.IOException; 6 /** 7 * 抛出异常 8 * @author acer 9 * 10 */ 11 public class throwexc { 12 public static void main(String[] args) 13 { 14 try { 15 String str; 16 str = new throwexc().openfile(); 17 System.out.println(str); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } 21 } 22 String openfile() throws FileNotFoundException,IOException 23 { 24 FileReader fr=new FileReader("d:a.txt"); 25char c=(char)fr.read(); 26return ""+c; 27 } 28 }
1 package array; 2 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.text.ParseException; 6 /** 7 * 方法重写声明异常原则 8 * @author acer 9 * 10 */ 11 class overexception{ 12 public void method() throws IOException{} 13} 14class b extends overexception{ 15publicvoid method() throws IOException{} 16} 17class c extends overexception{ 18publicvoid method() throws FileNotFoundException{} 19} 20class d extends overexception{ 21publicvoid method() throws IOException,ArithmeticException{} 22} 23class e extends overexception{ 24publicvoid method() throws IOException,RuntimeException{} 25} 26class f extends overexception{ 27publicvoid method() throws IOException,ParseException{} 28 }
8.
1 package array; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 6 public class handthrows { 7 public static void main(String[] args) 8 { 9 File fr=new File("d:/b.txt"); 10if(!fr.exists()) 11 { 12try{ 13thrownew FileNotFoundException("File is not exist!"); 14 }catch(Exception e){ 15 e.printStackTrace(); 16 } 17 } 18 } 19 }
运行结果:
java.io.FileNotFoundException: File is not exist!
at
array.handthrows.main(handthrows.java:13)
(说明:手动抛出异常用的不多)
9.
1 package array; 2 3 import java.io.IOException; 4 /** 5 * 自定义异常 6 * @author acer 7 * 8 */ 9 class MyException extends IOException{ 10public MyException(){ 1112 } 13public MyException(String message){ 14super(message); 15 } 16} 17publicclass myexception { 18publicstaticvoid main(String[] args) 19 { 20try{ 21new myexception().test(); 22 }catch(MyException e){ 23 } 24 } 25publicvoid test()throws MyException{} 26 }
10.
11.
原文:http://www.cnblogs.com/xiaojingang/p/3704120.html
内容总结
以上是互联网集市为您收集整理的java开始到熟悉72-76全部内容,希望文章能够帮你解决java开始到熟悉72-76所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。