java 异常处理机制及说明。
又抄袭了一篇文章,其实就是想保存到自己的博客中而已,文章出处:http://www.cnblogs.com/LilianChen/p/4639471.html
1. 如何捕获异常
try
{
可能会出现异常的代码段;
}
catch(异常类型名 处理该异常对象)
{
异常处理代码段;
}

1 import java.io.*;
2
3 public class TryCatchTest {
4
5 public static void main(String[] args) {
6 File file = new File("abc.txt");
7 int a[] = {1, 2};
8
9 try
10 {
11 System.out.println(3/0);
12 }
13 catch(ArithmeticException e1)
14 {
15 System.out.println("3/0: ");
16 System.out.println("This is ArithmeticException");
17 }
18
19 try
20 {
21 System.out.println(a[2]);
22 }
23 catch(ArrayIndexOutOfBoundsException e2)
24 {
25 System.out.println("a[2] is out of Array: ");
26 System.out.println("This is ArrayIndexOutOfBoundsException");
27 }
28
29 try
30 {
31 BufferedReader input = new BufferedReader(new FileReader(file));
32 }
33 catch (FileNotFoundException e3)
34 {
35 System.out.println("abc.txt is not found: ");
36 System.out.println("This is FileNotFoundException");
37 }
38 catch(IOException e)
39 {
40 System.out.println("This is IOException");
41 }
42
43 }
44
45 }

3/0:
This is ArithmeticException
a[2] is out of Array:
This is ArrayIndexOutOfBoundsException
abc.txt is not found:
This is FileNotFoundException
2. 如何抛出异常
编写代码过程中,如果不想在这段代码中捕捉和处理一个可能出现的异常,那么就需要将这个异常传递出去,传递给调用它的方法去处理该异常。这个时候就需要使用throw 和throws
- throws语句在方法声明中使用,抛出异常
- throw语句在方法体内部使用,抛出异常
注意: 方法体中若使用了throw语句抛出异常,则必须在该方法声明中,采用throws语句来声明该方法体中抛出的异常,同时,throws语句声明抛出的异常,必须是方法体中throw语句抛出的异常或该异常的父类。

1 import java.io.*;
2
3 public class ThrowTest {
4
5 public void throwTest1() throws ArithmeticException
6 {
7 System.out.println(3/0);
8 }
9
10 public void throwTest2() throws ArrayIndexOutOfBoundsException
11 {
12 int a[] ={1,2};
13 System.out.println(a[2]);
14 }
15
16 public void throwTest3() throws FileNotFoundException
17 {
18 File file=new File("abc.txt");
19 new BufferedReader(new FileReader(file));
20 }
21
22 public void throwTest4() throws FileNotFoundException
23 {
24 throw new FileNotFoundException("abc.txt");
25 }
26
27 public static void main(String[] args) {
28 ThrowTest throwTest=new ThrowTest();
29
30 try
31 {
32 throwTest.throwTest1();
33 }
34 catch (ArithmeticException e1)
35 {
36 System.out.println("3/0: ");
37 System.out.println("This is ArithmeticException");
38 }
39
40 try
41 {
42 throwTest.throwTest2();
43 }
44 catch(ArrayIndexOutOfBoundsException e2)
45 {
46 System.out.println("a[2] is out of Array: ");
47 System.out.println("This is ArrayIndexOutOfBoundsException");
48 }
49
50 try
51 {
52 throwTest.throwTest3();
53 }
54 catch (FileNotFoundException e3)
55 {
56 System.out.println("abc.txt is not found: ");
57 System.out.println("This is FileNotFoundException");
58 }
59
60 try
61 {
62 throwTest.throwTest4();
63 }
64 catch (FileNotFoundException e3)
65 {
66 System.out.println("abc.txt is not found: ");
67 System.out.println("This is FileNotFoundException");
68 }
69
70 }
71
72 }

3/0:
This is ArithmeticException
a[2] is out of Array:
This is ArrayIndexOutOfBoundsException
abc.txt is not found:
This is FileNotFoundException
abc.txt is not found:
This is FileNotFoundException
3. 自定义异常
建立自己的异常类,要做的只是根据需要,从Exception类或者从Exception类的子类中继承出需要的类。习惯上,会经常为每一个异常类,提供一个默认的和一个包含详细信息的构造器。需要注意的是,自定义异常类,必须由程序员使用throw语句抛出。

1 public class MyException {
2
3 public static void main(String[] args) {
4 String str="2abcde";
5
6 try
7 {
8 char c=str.charAt(0);
9 if(c<'a'||c>'z'||c<'A'||c>'Z')
10 throw new FirstLetterException();
11 }
12 catch (FirstLetterException e)
13 {
14 System.out.println("This is FirstLetterException");
15 }
16
17 }
18
19 }
20
21 class FirstLetterException extends Exception{
22 public FirstLetterException()
23 {
24 super("The first char is not a letter");
25 }
26
27 public FirstLetterException(String str)
28 {
29 super(str);
30 }
31 }

This is FirstLetterException

1 public class MyException {
2
3 public static void main(String[] args) throws FirstLetterException{
4 throw new FirstLetterException();
5 }
6 }
7
8 class FirstLetterException extends Exception{
9 public FirstLetterException()
10 {
11 super("The first char is not a letter");
12 }
13
14 public FirstLetterException(String str)
15 {
16 super(str);
17 }
18 }

Exception in thread "main" FirstLetterException: The first char is not a letter
at MyException.main(MyException.java:5)
4. 使用finally语句
在使用try...catch语句是,若try语句中的某一句出现异常情况,那么这部分try语句段中,从出现异常的语句开始,之后的所有语句都不会被执行,直到这部分try语句段结束。
但是在很多情况下,希望无论是否出现异常,某些语句都需要被执行。那么就可以把这部分代码放在finally语句段中,即使try或catch语句段中含有return语句,程序都会在异常抛出后先执行finally语句段,除非try或catch语句段中执行System.exit()方法,或者是出现Error错误,finally语句才不会被执行而退出程序。

1 import java.io.*;
2
3 public class FinallyTest {
4
5 public static void main(String[] args) {
6 File file=null;
7 BufferedReader input=null;
8 file=new File("abc.txt");
9
10 try
11 {
12 input=new BufferedReader(new FileReader(file));
13 }
14 catch(FileNotFoundException e)
15 {
16 System.out.print("abc.txt is not found: ");
17 System.out.println("This is FileNotFoundException");
18 }
19 finally
20 {
21 System.out.println("This is finally code part.");
22 }
23
24 }
25
26 }

abc.txt is not found: This is FileNotFoundException
This is finally code part.
java 异常处理机制及说明。的更多相关文章
- JAVA 异常处理机制
主要讲述几点: 一.异常的简介 二.异常处理流程 三.运行时异常和非运行时异常 四.throws和throw关键字 一.异常简介 异常处理是在程序运行之中出现的情况,例如除数为零.异常类(Except ...
- Java 异常处理机制和集合框架
一.实验目的 掌握面向对象程序设计技术 二.实验环境 1.微型计算机一台 2.WINDOWS操作系统,Java SDK,Eclipse开发环境 三.实验内容 1.Java异常处理机制涉及5个关键字:t ...
- java异常处理机制 (转载)
java异常处理机制 本文来自:曹胜欢博客专栏.转载请注明出处:http://blog.csdn.net/csh624366188 异常处理是程序设计中一个非常重要的方面,也是程序设计的一大难点,从C ...
- Java异常处理机制 —— 深入理解与开发应用
本文为原创博文,严禁转载,侵权必究! Java异常处理机制在日常开发中应用频繁,其最主要的不外乎几个关键字:try.catch.finally.throw.throws,以及各种各样的Exceptio ...
- 如何正确使用Java异常处理机制
文章来源:leaforbook - 如何正确使用Java异常处理机制作者:士别三日 第一节 异常处理概述 第二节 Java异常处理类 2.1 Throwable 2.1.1 Throwable有五种构 ...
- 【转】深入理解java异常处理机制
深入理解java异常处理机制 ; int c; for (int i = 2; i >= -2; i--) { c = b / i; System.out.println("i=&qu ...
- Java异常处理机制的秘密
一.结论 这些结论你可能从未听说过,但其正确性是毋庸置疑的,不妨先看看: 1.catch中throw不一定能抛回到上一层,因为finally中的return会抑制这个throw 2.finally中t ...
- Java异常处理机制及两种异常的区别
java异常处理机制主要依赖于try,catch,finally,throw,throws五个关键字. try 关键字后紧跟一个花括号括起来的代码块,简称try块.同理:下面的也被称为相应的块. ...
- java异常处理机制详解
java异常处理机制详解 程序很难做到完美,不免有各种各样的异常.比如程序本身有bug,比如程序打印时打印机没有纸了,比如内存不足.为了解决这些异常,我们需要知道异常发生的原因.对于一些常见的异常,我 ...
- Java异常处理机制 try-catch-finally 剖析
Java拥有着强大的异常处理机制,最近初步学习了下,感觉内容还是挺多的,特此来将自己的理解写出来与大家分享. 一. 在Java代码code中,由于使用Myeclipse IDE,可以自动提醒用户哪里有 ...
随机推荐
- Ubuntu Ruby On Rails安装和配置
在这篇文章中ubuntu通过rvm安装ruby和rails.步借鉴了官方网站和网上信息,这里给大家分享. 1. 安装mapapis公钥: gpg --keyserver hkp://keys.gnup ...
- Canvas入门(3):图像处理和渲染文本
资源:http://www.ido321.com/997.html 一.图像处理(非特别说明,全部结果均来自最新版Google) 在HTML 5中,不仅能够使用Canvas API绘制图形,也能够用于 ...
- ser2net使用
在ubuntu下或者openwrt下安装了ser2net程序之后,可以将串口中的数据转发为以太网数据. 设置在/etc/ser2net.conf中最后: 3002:0:/dv/ttyUSB0:1152 ...
- 解决IIS7 HTTP/405 Method Not Allowed 问题的方法.
1.处理程序映射 2.添加脚本映射 3.请求路径:*.html 4.可执行文件:C:/windows/system32/inetsrv/asp.dll 5.请求限制-谓词:输入需要允许请求的谓词(po ...
- 【SSRS】入门篇(二) -- 建立数据源
原文:[SSRS]入门篇(二) -- 建立数据源 通过 [SSRS]入门篇(一) -- 创建SSRS项目 这篇,我们建立了一个SSRS项目: 接下来,我们以 AdventureWorks2012 示例 ...
- python购物淫秽数据分析(2)
淘宝大数据的游戏,我重新提高自己的思维方式, 插件和代码前前后后写在六个版本,但最好的结果其实是我的第一次2第二码.这让我很惊讶, 但它也说明了一个问题.当你更熟悉的语言,当一方,你缺少的是其他的知识 ...
- Web Api 自动生成帮助文档
Web Api 自动生成帮助文档 新建Web Api项目之后,会在首页有API的导航菜单,点击即可看到API帮助文档,不过很遗憾,Description 是没有内容的. 怎么办呢? 第一步: 如果 ...
- 《C语言 学生成绩管理系统》
/* (盯着先拔头筹程序) * 该计划的版权声明和版本号 * Copyright (c) 2011, 烟台大学计算机学院学生的学校 * All rights reserved. * 文件名: 学生成绩 ...
- [转]How To Use CSS3 Media Queries To Create a Mobile Version of Your Website
CSS3 continues to both excite and frustrate web designers and developers. We are excited about the p ...
- Java类之间的关联关系(转载)
Java类之间的关联关系 UML类图中的关系分为四种:泛化.依赖.关联.实现:关联关系又可以细化为聚合和组合. 一.泛化(Generalization) 泛化是父类和子类之间的关系,子类继承父类的所有 ...