package com.imooc.exception;

import java.util.Scanner;

public class TryCatchDemo1 {

    public static void main(String[] args) {
// 要求输入两个数,输出两数之商
Scanner input=new Scanner(System.in);
System.out.println("========运算开始==========");
try {
System.out.println("请输入第一个整数:");
int one=input.nextInt();
System.out.println("请输入第二个整数:");
int two=input.nextInt();
System.out.println("one和two的商是:"+(one/two));
}catch(Exception e) {
System.out.println("输入内容错误!");
} System.out.println("========运算结束=========="); } }

二、try-catch-finally

1、简单处理异常

2、打印出错信息

3、finally处理善后

package com.imooc.exception;

import java.util.Scanner;

public class TryCatchDemo1 {

    public static void main(String[] args) {
// 要求输入两个数,输出两数之商
Scanner input=new Scanner(System.in);
System.out.println("========运算开始==========");
try {
System.out.println("请输入第一个整数:");
int one=input.nextInt();
System.out.println("请输入第二个整数:");
int two=input.nextInt();
System.out.println("one和two的商是:"+(one/two));
}catch(Exception e) {
//1、处理错误,简单输出提示信息
System.out.println("输入内容错误!");
//2、打印错误信息
e.printStackTrace();
}finally {
//3、finally块释放资源
System.out.println("========运算结束==========");
} } }

三、try-catch-catch-finaly

1、对不同类别的异常分别处理

2、最后增加异常父类Exception

package com.imooc.exception;

import java.util.InputMismatchException;
import java.util.Scanner; public class TryCatchDemo1 { public static void main(String[] args) {
// 要求输入两个数,输出两数之商
Scanner input=new Scanner(System.in);
System.out.println("========运算开始==========");
try {
System.out.println("请输入第一个整数:");
int one=input.nextInt();
System.out.println("请输入第二个整数:");
int two=input.nextInt();
System.out.println("one和two的商是:"+(one/two));
}catch(ArithmeticException e) {
//1、算术异常处理
System.out.println("除数不能为零!");
//2、打印错误信息
e.printStackTrace();
}catch(InputMismatchException e) {
//3、输入异常处理
System.out.println("请输入整数!");
e.printStackTrace(); }catch(Exception e) {
//4、其他错误异常处理
System.out.println("程序出错了!");
e.printStackTrace();
}
finally {
//3、finally块释放资源
System.out.println("========运算结束==========");
} } }

四、return语句

package com.imooc.exception;

import java.util.InputMismatchException;
import java.util.Scanner; /*
* 带return语句的异常处理
*/
public class TryCatchRetrun { public static void main(String[] args) {
TryCatchRetrun tcr=new TryCatchRetrun();
int result=tcr.test();
System.out.println("两个数的商是:"+result); }
public static int test() {
Scanner input=new Scanner(System.in);
System.out.println("========运算开始==========");
try {
System.out.println("请输入第一个整数:");
int one=input.nextInt();
System.out.println("请输入第二个整数:");
int two=input.nextInt();
return one/two;
}catch(ArithmeticException e) {
//1、算术异常处理
System.out.println("除数不能为零!");
return 0;
}finally {
//3、finally块释放资源
System.out.println("========运算结束==========");
//finally中的return语句会屏蔽try和catch块中的return语句,所以不建议在finally中加入return语句
//return -10000;
}
} }

五、throws声明异常

1、throws后面接多个异常类型,中间用逗号分隔

package com.imooc.exception;

import java.util.InputMismatchException;
import java.util.Scanner; /*
* 1、throws声明多种异常类型,但不处理异常
* 2、由调用者针对不同异常类型,进行处理
*/
public class ThrowsDemo { public static void main(String[] args) { try {
int result=test();
System.out.println("两个数的商是:"+result);
}catch (ArithmeticException e) {
// 处理算术异常
System.out.println("除数不能为零!");
e.printStackTrace();
}catch (InputMismatchException e) {
// 处理输入异常
System.out.println("必须输入整数!");
e.printStackTrace();
} }
public static int test() throws ArithmeticException,InputMismatchException {
Scanner input=new Scanner(System.in);
System.out.println("========运算开始==========");
System.out.println("请输入第一个整数:");
int one=input.nextInt();
System.out.println("请输入第二个整数:");
int two=input.nextInt();
System.out.println("========运算结束==========");
return one/two; } }

2、throws后面接Exception

package com.imooc.exception;

import java.util.InputMismatchException;
import java.util.Scanner; /*
* 1、throws后面接Exception
*/
public class ThrowsDemo2 { public static void main(String[] args) { try {
int result=test();
System.out.println("两个数的商是:"+result);
}catch (ArithmeticException e) {
// 处理算术异常
System.out.println("除数不能为零!");
e.printStackTrace();
}catch (InputMismatchException e) {
// 处理输入异常
System.out.println("必须输入整数!");
e.printStackTrace();
}catch(Exception e) {
System.out.println("程序出错误了!");
e.printStackTrace();
} }
public static int test() throws Exception {
Scanner input=new Scanner(System.in);
System.out.println("========运算开始==========");
System.out.println("请输入第一个整数:");
int one=input.nextInt();
System.out.println("请输入第二个整数:");
int two=input.nextInt();
System.out.println("========运算结束==========");
return one/two; } }

3、为非检查异常增加文档注释

Exception为检查异常,声明后,编译器会提示必须处理;

ArithmeticException,InputMismatchException为非检查异常,编译器不会强制要求,可以使用文档注释进行提示

package com.imooc.exception;

import java.util.InputMismatchException;
import java.util.Scanner; public class ThrowsDemo2 { public static void main(String[] args) { try {
int result=test();
System.out.println("两个数的商是:"+result);
}catch (ArithmeticException e) {
// 处理算术异常
System.out.println("除数不能为零!");
e.printStackTrace();
}catch (InputMismatchException e) {
// 处理输入异常
System.out.println("必须输入整数!");
e.printStackTrace();
}catch(Exception e) {
System.out.println("程序出错误了!");
e.printStackTrace();
}
test(); }
/**
* 测试接收数据相除结果的方法
* @return 两个数据的商
* @throws ArithmeticException
* @throws InputMismatchException
*/
public static int test() throws ArithmeticException,InputMismatchException {
Scanner input=new Scanner(System.in);
System.out.println("========运算开始==========");
System.out.println("请输入第一个整数:");
int one=input.nextInt();
System.out.println("请输入第二个整数:");
int two=input.nextInt();
System.out.println("========运算结束==========");
return one/two; } }

六、throw抛出异常

1、通过try-catch包含throw语句,自己抛出自己处理

package com.imooc.exception;

import java.util.Scanner;

public class ThrowDemo {

    public static void main(String[] args) {
// TODO Auto-generated method stub
testAge(); }
/**
* 描述酒店入住规则:限定年龄,18岁以下,80岁以上的住客必须由亲友陪同
* 1、throw自己抛出自己处理
*/
public static void testAge() {
try {
System.out.println("请输入年龄:");
Scanner input = new Scanner(System.in);
int age=input.nextInt();
if(age<18||age>80) {
throw new Exception("18岁以下,80岁以上的住客必须由亲友陪同!");
}else {
System.out.println("欢迎入住酒店");
} }catch(Exception e) {
e.printStackTrace();
} }
}

2、通过throws在方法声明处抛出异常类型——谁调用谁处理——调用者可以自己处理,也可以继续往上抛

package com.imooc.exception;

import java.util.Scanner;

public class ThrowDemo2 {

    public static void main(String[] args) {
// TODO Auto-generated method stub
try {
testAge();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
/**
* 描述酒店入住规则:限定年龄,18岁以下,80岁以上的住客必须由亲友陪同
* 1、通过try-catch包含throw语句,自己抛出自己处理
* 2、通过throws在方法声明处抛出异常类型——谁调用谁处理——调用者可以自己处理,也可以继续往上抛
* @throws Exception
*/
public static void testAge() throws Exception { System.out.println("请输入年龄:");
Scanner input = new Scanner(System.in);
int age=input.nextInt();
if(age<18||age>80) {
throw new Exception("18岁以下,80岁以上的住客必须由亲友陪同!");
}else {
System.out.println("欢迎入住酒店");
} }
}

3、注意事项

1)throw建议抛出检查类型异常,不建议抛出非检查类型异常

2)throws声明的异常类型可以是throw抛出的异常同类型,或者父类型,不能是子类型

七、自定义异常

1、自定义异常类继承Exception

package com.imooc.exception;

//自定义异常类
public class HotelAgeException extends Exception {
public HotelAgeException() {
super("18岁以下,80岁以上的住客必须由亲友陪同!");
}
}

2、抛出自定义类,并声明处理

package com.imooc.exception;

import java.util.Scanner;

public class ThrowDemo3 {

    public static void main(String[] args) {

        try {
testAge();
} catch (HotelAgeException e) {
System.out.println(e.getMessage());
System.out.println("工作人员不允许办理入住!");
e.printStackTrace();
} } public static void testAge() throws HotelAgeException { System.out.println("请输入年龄:");
Scanner input = new Scanner(System.in);
int age=input.nextInt();
if(age<18||age>80) {
throw new HotelAgeException();
}else {
System.out.println("欢迎入住酒店");
} }
}

八、异常链

package com.imooc.exception;

public class ThrowDemo4 {

    public static void main(String[] args) {
// TODO Auto-generated method stub
try {
testThree();
} catch (Exception e) {
e.printStackTrace();
} }
public static void testOne() throws HotelAgeException {
throw new HotelAgeException(); }
public static void testTwo() throws Exception {
try {
testOne();
} catch (HotelAgeException e) {
throw new Exception("我是新产生的异常1!",e);
}
}
public static void testThree() throws Exception {
try {
testTwo();
} catch (Exception e) {
Exception e1=new Exception("我是新产生的异常2!");
e1.initCause(e);
throw e1;
// throw new Exception();
} }
}

Java常用工具——java异常的更多相关文章

  1. Java常用工具——java字符串

    一.String常用字符串 package com.imooc.string; public class StringDemo { public static void main(String[] a ...

  2. Java常用工具——java集合

    一.ArrayList package com.imooc.set; import java.util.ArrayList; import java.util.List; public class A ...

  3. Java常用工具——java多线程

    一.线程的创建 方式一:继承Thread类,重写run()方法 package com.imooc.thread1; class MyThread extends Thread{ public MyT ...

  4. Java常用工具——java包装类

    一.包装类和基本数据类型 装箱:基本数据类型——包装类 拆箱:包装类——基本数据类型 package com.imooc.wrap; public class WrapTestOne { public ...

  5. JavaEE-实验一 Java常用工具类编程

    该博客仅专为我的小伙伴提供参考而附加,没空加上代码具体解析,望各位谅解 1.  使用类String类的分割split 将字符串  “Solutions to selected exercises ca ...

  6. Java常用工具+类库合集

    1 常用工具 JVisual vm:可以直接通过软件包下载,支持本地以及远程JVM监控 JMH:Java Microbenchmark Harness,测试基准组件,精度可达纳秒级 JITWatch: ...

  7. Java常用工具类题库

    一.    填空题 在Java中每个Java基本类型在java.lang包中都在一个相应的包装类,把基本类型数据转换为对象,其中包装类Integer是___Number__的直接子类. 包装类Inte ...

  8. JAVA(三)JAVA常用类库/JAVA IO

    成鹏致远 | lcw.cnblog.com |2014-02-01 JAVA常用类库 1.StringBuffer StringBuffer是使用缓冲区的,本身也是操作字符串的,但是与String类不 ...

  9. JAVA常用工具类汇总

    一.功能方法目录清单: 1.getString(String sSource)的功能是判断参数是否为空,为空返回"",否则返回其值: 2.getString(int iSource ...

随机推荐

  1. 获取文件夹中前N个文件

    @echo off set input="list.txt" set srcDir="%1" set /a fileCount=10 set /a curInd ...

  2. Greg and Array CodeForces 296C 差分数组

    Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...

  3. Codeforces 1042C (贪心+模拟)

    题面 传送门 分析 思路简单,但代码较复杂的贪心 分类讨论: 有0 负数有奇数个:将绝对值最小(实际最大)的负数和0全部乘到一起,最后删掉0 负数有偶数个:将0全部乘到一起,最后删掉0 没有0 负数有 ...

  4. 不定参数(rest 参数 ...)

    不定参数 如何实现不定参数 使用过 underscore.js 的人,肯定都使用过以下几个方法: _.without(array, *values) //返回一个删除所有values值后的array副 ...

  5. js如何实现上拉加载更多...

    我们在项目中经常使用到下拉加载更多,之前要么是底部写加载按钮,要么是引入插件.今天终于有时间手写一个了,之前感觉挺麻烦,明白原理后,其实很简单... scrollTop:滚动视窗的高度距离window ...

  6. P1828 香甜的黄油 (spfa)

    [题目描述] 农夫John知道每只奶牛都在各自喜欢的牧场(一个牧场不一定只有一头牛).给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那). [题目链接] https ...

  7. javascript(DOM)实例

    JavaScript学习笔记 JS补充笔记 实例之跑马灯,函数创建.通过ID获取标签及内部的值,字符串的获取与拼接.定时器的使用 使用定时器实现在console中打印内容 Dom选择器使用与调试记录 ...

  8. CSS的重用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. asp.net 几种传值方法的分析

    本文转自:http://www.cnblogs.com/shengtianlong/archive/2010/08/11/1797608.html ASP.NET页面传值方法的优缺点及适用范围 1. ...

  10. Java缓存Ehcache-Ehcache的Cache在SSM框架中的配置

    需要在Spring配置文件中配置: <!-- 配置缓存管理器工厂 --> <bean id="cacheManager" class="org.spri ...