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,可以自动提醒用户哪里有 ...
随机推荐
- 【UVA】10285-Longest Run on a Snowboard(动态规划)
这是一个简单的问题.你并不需要打印路径. 状态方程dp[i][j] = max(dp[i-1][j],dp[i][j-1],dp[i+1][j],dp[i][j+1]); 14003395 10285 ...
- 18 个最新实用的 jQuery 插件
1. Simple Effects for Drop-Down Lists 一个jQuery插件用于将普通的select控件转成一个带有一些简单扩展效果的下拉列表. 2. X-editable 这个插 ...
- 读书笔记—CLR via C#章节3
这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可以加深 ...
- 使用winform控件注意线程绘制界面冲突
在用.NET Framework框架的WinForm构建GUI程序界面时,如果要在控件的事件响应函数中改变控件的状态,例如:某个按钮上的文本原先叫“打开”,单击之后按钮上的文本显示“关闭”,初学者往往 ...
- MVC Sesion丢失问题
花了两个小时的时间处理一个Session 取值问题 使用MVC LoadCheckController(登录校验)登录成功后创建一个Session,Session中封装了用户的相关信息如权限基本信息, ...
- css,js工具篇
4. web前端开发分享-css,js工具篇 web前端开发乃及其它的相关开发,推荐sublime text, webstorm(jetbrains公司系列产品)这两个的原因在于,有个技术叫emm ...
- 记录OC学习的一点一滴(一)
练习代码: 代码一:(面向过程的OC) // // main.m // Class01Test01 // // Copyright (c) 2014年 levi. All rights reserve ...
- iOS基础 - 瀑布流
一.瀑布流简介 瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pint ...
- 测试驱动 ASP.NET MVC 和构建可测试 ASP.NET MVC 应用程序
[测试驱动 ASP.NET MVC] http://t.cn/8kdi4Wl [构建可测试 ASP.NET MVC 应用程序]http://t.cn/8kdi4Wj
- nutch solr 配置
http://blog.csdn.net/panjunbiao/article/details/12171147 后半部分实践通过