java开始到熟悉72-76
本次内容:异常机制
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的更多相关文章
- Effective Java 第三版——72. 赞成使用标准异常
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- 20165304实验一java开发环境熟悉
实验报告封面 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:李松杨 学号:20165304 指导教师:娄嘉鹏 实验日期:2018年4月2日 实验时间:13:45 - 15:25 实 ...
- 20165320 实验一 java环境的熟悉
实验内容与步骤 一.java开发环境的熟悉 1.建立一个有关自己学号的目录 2.在当前文件下编译一个带包Hello.java文件 3.代码内容 package sq; import java.util ...
- java开始到熟悉100-102
本次内容:arraylist() 1. package list; import java.util.ArrayList; import java.util.Date; import java.uti ...
- 慕课网-Java入门第一季-7-2 Java 中无参无返回值方法的使用
来源:http://www.imooc.com/code/1578 如果方法不包含参数,且没有返回值,我们称为无参无返回值的方法. 方法的使用分两步: 第一步,定义方法 例如:下面代码定义了一个方法名 ...
- java基础:熟悉3种内部类的写法,重点匿名内部类的使用
一.内部类定义 内部类(nested classes),面向对象程序设计中,可以在一个类的内部定义另一个类.嵌套类分为两种,即静态嵌套类和非静态嵌套类.静态嵌套类使用很少,最重要的是非静态嵌套类,也即 ...
- [原创]java WEB学习笔记72:Struts2 学习之路-- 文件的上传下载,及上传下载相关问题
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Java基础知识强化72:正则表达式之判断功能(手机号码判断 和 校验邮箱)
1. 判断功能: 使用了String类的matches方法,如下: public boolean matches(String regex): 2. 判断手机号码的案例: package cn.it ...
- Java知多少(72)文件的随机读写
Java.io 包提供了 RandomAccessFile 类用于随机文件的创建和访问.使用这个类,可以跳转到文件的任意位置读写数据.程序可以在随机文件中插入数据,而不会破坏该文件的其他数据.此外,程 ...
- Java编程的逻辑 (72) - 显式条件
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
随机推荐
- PAT Basic 1042
1042 字符统计 请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过 1000 的字符串.字符串由 ASCII 码表中任意可见字符及空格组成,至少包 ...
- DocView mode 0 -- 介绍
DocView mode,可作为主模式也可以作为minor mode,可以用来阅读DVI(ps后缀),PDF,OpenDocument(libreoffice文档),微软的doc.支持截取 ...
- go 本地安装 grpc-go
https://blog.csdn.net/code_segment/article/details/77461590 https://github.com/grpc/grpc-go git clon ...
- js--进阶知识点
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python第三方库之openpyxl(8)
Python第三方库之openpyxl(8) 饼图 饼图将数据绘制成一个圆片,每个片代表整体的百分比.切片是按顺时针方向绘制的,0在圆的顶部.饼图只能取一组数据.该图表的标题将默认为该系列的标题. 2 ...
- python3--命名空间字典
命名空间字典 我们学到了模块的命名空间实际上是以字典的形式实现的,并且可以由内置属性__dict__显示这一点.类和实例对象也是如此:属性点号运算其实内部就是字典的索引运算,而属性继承其实就是搜索连结 ...
- javascript异常cannot read property xx of null 的错误
一般报这种异常或者错误,是因为试图从null中再读一个属性导致的. 比如:var myAttr=myObj.data.Name; 假如这个时候myObj.data是null,那么再试图读取data的N ...
- Web开发细节搜集
App_Data 百度百科: App_Data文件夹应该包含应用程序的本地数据存储.它通常以文件(诸如Microsoft Access或Microsoft SQL Server Express数据库 ...
- 九度oj 题目1107:搬水果
题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果合成一堆.每一次合并,小明可以把两堆水果合并到一起,消耗的体力等于两堆水果的重量之和.当然经 ...
- jenkins之Tomcat7+jdk1.7+jenkins
目的 在开发中,需要经常频繁的对测试服务器进行部署,而且在多人协同中开发经常遇到的问题就是别人更新了他的代码,而你去更新你的代码时并没有更新到别人的代码,导致测试环境的代码不是最新,当然这个问题也好解 ...