本次内容:异常机制

1、为什么需要异常

2、异常

3、error类

4、exception类

5、exception类中的unchecked exception

举例:

6、常用异常处理方法

a.try

注意:一个try语句块至少得带一个finally语句块或catch语句块

 package array;
/**
* exception
* @author acer
*
*/
public class exception {
public static void main(String[] args)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
 package array;
/**
* try catch finally
*/
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; public class trycatch {
public static void main(String[] args)
{
FileReader f=null;
try {
f=new FileReader("d:/a.txt");
char c,d;
c = (char)f.read();
d = (char)f.read();
System.out.println(""+c+d);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(f!=null)
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

运行结果:
ab

(说明:a.txt文本中的内容:abcdefg)

不同内容:try,catch,finally,return执行顺序

代码1:

 package array;

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; /**
* 测试try,catch,finally,return执行顺序
* @author acer
*
*/
public class exeshunxu {
public static void main(String[] args)
{
String str=new exeshunxu().openfile();
System.out.println(str);
}
String openfile()
{
try {
System.out.println("aaa");
FileInputStream fi=new FileInputStream("d:/a.txt");
int a=fi.read();
System.out.println("bbb");
return "try"; } catch (FileNotFoundException e) {
System.out.println("catchingchild..................");
e.printStackTrace();
return "catch filenotfoundexception";
} catch (IOException e) {
System.out.println("catchingfather................");
e.printStackTrace();
return "catch ioexception";
}finally{
System.out.println("finally...............");
return "finally";
}
}
}

运行结果:
aaa
bbb
finally...............
finally

代码2:

 package array;

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; /**
* 测试try,catch,finally,return执行顺序
* @author acer
*
*/
public class exeshunxu {
public static void main(String[] args)
{
String str=new exeshunxu().openfile();
System.out.println(str);
}
String openfile()
{
try {
System.out.println("aaa");
FileInputStream fi=new FileInputStream("d:/a.txt");
int a=fi.read();
System.out.println("bbb");
return "try"; } catch (FileNotFoundException e) {
System.out.println("catchingchild..................");
e.printStackTrace();
return "catch filenotfoundexception";
} catch (IOException e) {
System.out.println("catchingfather................");
e.printStackTrace();
return "catch ioexception";
}finally{
System.out.println("finally...............");
}
}
}

运行结果:
aaa
bbb
finally...............
try

代码3:

 package array;

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; /**
* 测试try,catch,finally,return执行顺序
* @author acer
*
*/
public class exeshunxu {
public static void main(String[] args)
{
String str=new exeshunxu().openfile();
System.out.println(str);
}
String openfile()
{
try {
System.out.println("aaa");
FileInputStream fi=new FileInputStream("d:/abcd.txt");
int a=fi.read();
System.out.println("bbb");
return "try"; } catch (FileNotFoundException e) {
System.out.println("catchingchild..................");
e.printStackTrace();
return "catch filenotfoundexception";
} catch (IOException e) {
System.out.println("catchingfather................");
e.printStackTrace();
return "catch ioexception";
}finally{
System.out.println("finally...............");
}
}
}

运行结果:
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.

 package array;

 import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* 抛出异常
* @author acer
*
*/
public class throwexc {
public static void main(String[] args)
{
try {
String str;
str = new throwexc().openfile();
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
String openfile() throws FileNotFoundException,IOException
{
FileReader fr=new FileReader("d:a.txt");
char c=(char)fr.read();
return ""+c;
}
}

 package array;

 import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
/**
* 方法重写声明异常原则
* @author acer
*
*/
class overexception{
public void method() throws IOException{}
}
class b extends overexception{
public void method() throws IOException{}
}
class c extends overexception{
public void method() throws FileNotFoundException{}
}
class d extends overexception{
public void method() throws IOException,ArithmeticException{}
}
class e extends overexception{
public void method() throws IOException,RuntimeException{}
}
class f extends overexception{
public void method() throws IOException,ParseException{}
}

8.

 package array;

 import java.io.File;
import java.io.FileNotFoundException; public class handthrows {
public static void main(String[] args)
{
File fr=new File("d:/b.txt");
if(!fr.exists())
{
try{
throw new FileNotFoundException("File is not exist!");
}catch(Exception e){
e.printStackTrace();
}
}
}
}

运行结果:
java.io.FileNotFoundException: File is not exist!
 at array.handthrows.main(handthrows.java:13)

(说明:手动抛出异常用的不多)

9.

 package array;

 import java.io.IOException;
/**
* 自定义异常
* @author acer
*
*/
class MyException extends IOException{
public MyException(){ }
public MyException(String message){
super(message);
}
}
public class myexception {
public static void main(String[] args)
{
try{
new myexception().test();
}catch(MyException e){
}
}
public void test()throws MyException{}
}

10.

11.

java开始到熟悉72-76的更多相关文章

  1. Effective Java 第三版——72. 赞成使用标准异常

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  2. 20165304实验一java开发环境熟悉

    实验报告封面 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:李松杨 学号:20165304 指导教师:娄嘉鹏 实验日期:2018年4月2日 实验时间:13:45 - 15:25 实 ...

  3. 20165320 实验一 java环境的熟悉

    实验内容与步骤 一.java开发环境的熟悉 1.建立一个有关自己学号的目录 2.在当前文件下编译一个带包Hello.java文件 3.代码内容 package sq; import java.util ...

  4. java开始到熟悉100-102

    本次内容:arraylist() 1. package list; import java.util.ArrayList; import java.util.Date; import java.uti ...

  5. 慕课网-Java入门第一季-7-2 Java 中无参无返回值方法的使用

    来源:http://www.imooc.com/code/1578 如果方法不包含参数,且没有返回值,我们称为无参无返回值的方法. 方法的使用分两步: 第一步,定义方法 例如:下面代码定义了一个方法名 ...

  6. java基础:熟悉3种内部类的写法,重点匿名内部类的使用

    一.内部类定义 内部类(nested classes),面向对象程序设计中,可以在一个类的内部定义另一个类.嵌套类分为两种,即静态嵌套类和非静态嵌套类.静态嵌套类使用很少,最重要的是非静态嵌套类,也即 ...

  7. [原创]java WEB学习笔记72:Struts2 学习之路-- 文件的上传下载,及上传下载相关问题

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  8. Java基础知识强化72:正则表达式之判断功能(手机号码判断 和 校验邮箱)

    1.  判断功能: 使用了String类的matches方法,如下: public boolean matches(String regex): 2. 判断手机号码的案例: package cn.it ...

  9. Java知多少(72)文件的随机读写

    Java.io 包提供了 RandomAccessFile 类用于随机文件的创建和访问.使用这个类,可以跳转到文件的任意位置读写数据.程序可以在随机文件中插入数据,而不会破坏该文件的其他数据.此外,程 ...

  10. Java编程的逻辑 (72) - 显式条件

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

随机推荐

  1. LeetCode(108) Convert Sorted Array to Binary Search Tree

    题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...

  2. Verilog学习笔记基本语法篇(四)·········块语句

    块语句是指将两条或者两条以上的语句组合在一起,使其在格式上更像一条语句.块语句分为两种: 1)用begin_end语句,通常用来标识顺序执行的语句,用它标识的块称作顺序块: 2)用fork_join语 ...

  3. BZOJ 4809: 皇后

    题目大意: n皇后问题,有些格子不能放. 题解: 直接暴力,并不用加优化就能过. 代码: #include<cstdio> using namespace std; int cc,n,an ...

  4. angular controller与directive相互引用

    /** * Created by Administrator on 2017/8/28. */ var app =angular.module('app',[]); app.directive('fo ...

  5. HDU-5317 RGCDQ ,暴力打表!

    RGCDQ 暴力水题,很可惜比赛时没有做出来,理清思路是很简单的. 题意:定义f(i)表示i的素因子个数,给你一段区间[l,r],求max_gcd(f(i),f(j)).具体细节参考题目. 思路:数据 ...

  6. 九度oj 题目1135:字符串排序

    题目描述: 先输入你要输入的字符串的个数.然后换行输入该组字符串.每个字符串以回车结束,每个字符串少于一百个字符. 如果在输入过程中输入的一个字符串为“stop”,也结束输入. 然后将这输入的该组字符 ...

  7. BZOJ 2946 [Poi2000]公共串 ——后缀自动机

    任意选择一个串作为模式串,构建出后缀自动机. 然后用其他的串在后缀自动机上跑匹配. 然后就到了理解后缀自动机性质的时候. 在某一个节点的最大值是可以沿着parent树上传的. 然后用dp[i][j]表 ...

  8. 【2018.11.22】CTSC2018(模拟赛!)

    太蠢了……$noip$ 后第一次模拟赛竟然是这样的……完全就是打击自信 / 降智…… 1. 假面 一道神仙概率 $dp$!第一次写…… 拿到题就发现血量 $m_i$ 的上限只有 $100$! 然后 $ ...

  9. bzoj1411: [ZJOI2009]硬币游戏

    1411: [ZJOI2009]硬币游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 965  Solved: 420[Submit][Status ...

  10. linux文件夹作用

    linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...