本次内容:异常机制

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(112) Path Sum

    题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  2. LeetCode(109) Convert Sorted List to Binary Search Tree

    题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height b ...

  3. java 协程框架quasar gradle配置

    https://github.com/puniverse/quasar-gradle-template/blob/master/gradle/agent.gradle 1.将其中的"-jav ...

  4. Java-字符串大小写转换

    package com.tj; public class MyClass implements Cloneable { public static void main(String[] args) { ...

  5. STM32F407 ADC 个人笔记

    1. ADC概述(STM32F4xx系列) 3 个 ADC 可分别独立使用 也可使用双重/三重模式(提高采样率) 2 个通道组 规则通道:相当于正常运行的程序 注入通道:相当于中断(可以打断规则通道的 ...

  6. liunx 根目录介绍

    1. /bin binary二进制 存放系统许多可执行程序文件 执行的相关指令,例如ls pwd whoami,后台的支持文件目录 2. /sbin super binary超级的二进制 存放系统许多 ...

  7. win10 设置软件开机启动项失效

    问题重现: win10系统,只要是图标右下角带盾牌标志的软件,加入系统的启动文件夹:如:C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Sta ...

  8. 【工具】Homebrew的安装及使用

    Homebrew官网:http://brew.sh/index_zh-cn.html Homebrew是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,相当于linux下的a ...

  9. [Kubernetes]Pod字段自动填充

    PodPreset(Pod预设置)在Kubernetes v1.11以后出现,开发人员只需要提交一个基本的Pod YAML,Kubernetes就可以自动给对应的Pod对象加上运维人员设定好的其他必要 ...

  10. nginx反向代理+负载均衡+https

    A服务器(192.168.133.1)作为nginx代理服务器 B服务器(192.168.133.2)作为后端真实服务器 访问https://www.test.com请求从A服务器上反向代理到B服务器 ...