标签:style blog class code java c
本次内容:异常机制
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 block 14 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; 13 try { 14 f=new FileReader("d:/a.txt"); 15 char 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{ 24 try { 25 if(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 { 20 try { 21 System.out.println("aaa"); 22 FileInputStream fi=new FileInputStream("d:/a.txt"); 23 int a=fi.read(); 24 System.out.println("bbb"); 25 return "try"; 26 27 } catch (FileNotFoundException e) { 28 System.out.println("catchingchild.................."); 29 e.printStackTrace(); 30 return "catch filenotfoundexception"; 31 } catch (IOException e) { 32 System.out.println("catchingfather................"); 33 e.printStackTrace(); 34 return "catch ioexception"; 35 }finally{ 36 System.out.println("finally..............."); 37 return "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 { 20 try { 21 System.out.println("aaa"); 22 FileInputStream fi=new FileInputStream("d:/a.txt"); 23 int a=fi.read(); 24 System.out.println("bbb"); 25 return "try"; 26 27 } catch (FileNotFoundException e) { 28 System.out.println("catchingchild.................."); 29 e.printStackTrace(); 30 return "catch filenotfoundexception"; 31 } catch (IOException e) { 32 System.out.println("catchingfather................"); 33 e.printStackTrace(); 34 return "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 { 20 try { 21 System.out.println("aaa"); 22 FileInputStream fi=new FileInputStream("d:/abcd.txt"); 23 int a=fi.read(); 24 System.out.println("bbb"); 25 return "try"; 26 27 } catch (FileNotFoundException e) { 28 System.out.println("catchingchild.................."); 29 e.printStackTrace(); 30 return "catch filenotfoundexception"; 31 } catch (IOException e) { 32 System.out.println("catchingfather................"); 33 e.printStackTrace(); 34 return "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"); 25 char c=(char)fr.read(); 26 return ""+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 } 14 class b extends overexception{ 15 public void method() throws IOException{} 16 } 17 class c extends overexception{ 18 public void method() throws FileNotFoundException{} 19 } 20 class d extends overexception{ 21 public void method() throws IOException,ArithmeticException{} 22 } 23 class e extends overexception{ 24 public void method() throws IOException,RuntimeException{} 25 } 26 class f extends overexception{ 27 public void 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"); 10 if(!fr.exists()) 11 { 12 try{ 13 throw new 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{ 10 public MyException(){ 11 12 } 13 public MyException(String message){ 14 super(message); 15 } 16 } 17 public class myexception { 18 public static void main(String[] args) 19 { 20 try{ 21 new myexception().test(); 22 }catch(MyException e){ 23 } 24 } 25 public void test()throws MyException{} 26 }
10.
11.
标签:style blog class code java c
原文地址:http://www.cnblogs.com/xiaojingang/p/3704120.html