本次内容:异常机制

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. Solr通过配DIH对数据库数据做索引

    1 加入相关jar包 将2个相关jar包复制到/opt/solr-7.7.1/server/solr-webapp/webapp/WEB-INF/lib文件夹下 jar包名称 solr-dataimp ...

  2. initcall机制

    参考:initcall机制 /* include/linux/init.h: */ /* For assembly routines */ #define __HEAD .section " ...

  3. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  4. 【20】display,float,position的关系

    [20]display,float,position的关系 如果display为none,元素不显示. 否则,如果position值为absolute或者fixed,元素绝对定位,float的计算值为 ...

  5. Thread.getContextClassLoader() is null

    Java threads created from JNI code in a non-java thread have null ContextClassloader unless the crea ...

  6. [luoguP3608] [USACO17JAN]Balanced Photo平衡的照片(树状数组 + 离散化)

    传送门 树状数组裸题 #include <cstdio> #include <cstring> #include <iostream> #include <a ...

  7. 洛谷P3759 - [TJOI2017]不勤劳的图书管理员

    Portal Description 给出一个\(1..n(n\leq5\times10^4)\)的排列\(\{a_n\}\)和数列\(\{w_n\}(w_i\leq10^5)\),进行\(m(m\l ...

  8. jQuary的相关动画效果

    第一种:该方法隐藏所有 <p> 元素: <html> <head> <script type="text/javascript" src= ...

  9. [HDU-4825] Xor-Sum (01字典树)

    Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...

  10. P1681 最大正方形II (动态规划)

    题目背景 忙完了学校的事,v神终于可以做他的"正事":陪女朋友散步.一天,他和女朋友走着走着,不知不觉就来到了一个千里无烟的地方.v神正要往回走,如发现了一块牌子,牌子上有有一行小 ...