本次内容:异常机制

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. LCD驱动分析(二)帧缓冲设备作为平台设备

    参考:S3C2440 LCD驱动(FrameBuffer)实例开发<一>   S3C2440 LCD驱动(FrameBuffer)实例开发<二> 1.平台设备注册 1.1在li ...

  2. LeetCode(79) Word Search

    题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...

  3. 杭电 1503 Advanced Fruits

    Description The company "21st Century Fruits" has specialized in creating new sorts of fru ...

  4. BZOJ 2295: [POJ Challenge]我爱你啊

    由于是子序列,那么难度就在于读入 #include<cstdio> #include<algorithm> #include<cstring> using name ...

  5. [android开发篇]安装android sdk的时候请注意

    第二就是: 如果要国内镜像的话: 3.大连东软信息学院镜像服务器地址: http://mirrors.neusoft.edu.cn  端口:80 随便选择一个就行啦.这里我选择的是第三个站点,即大连东 ...

  6. 【Android】android:ellipsize的使用以及一个点解决方法

    EidtText和textview中内容过长的话自动换行,使用android:ellipsize与android:singleine可以解决,使只有一行. EditText不支持marquee 用法如 ...

  7. 雅礼培训 Problem A 【线段树】

    题意 维护一段区间,支持求区间最大值,区间且,区间或 \(n,q<=2*10^5\) 题解 我们用线段树维护区间最大值 对于and和or运算, and实质就是强行把一些位改为0 or实质就是强行 ...

  8. zoj 2562 反素数

    题目大意:求n范围内最大的反素数(反素数定义:f(x)表示x的因子数,f(x)>f(x1) (0<x1<x)) x用质因数形式为:x=a1^p1*a2^p2......an^pn(a ...

  9. Gty的妹子树(bzoj 3720)

    Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...

  10. 区间翻转(codevs 3243)

    题目描述 Description 给出N个数,要求做M次区间翻转(如1 2 3 4变成4 3 2 1),求出最后的序列 输入描述 Input Description 第一行一个数N,下一行N个数表示原 ...