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. Android之异步调用

    概述 AsyncTask可以很好的,准确的使用UI线程,他可以将一个比较耗时(几秒钟)的动作运行在后台,并且能将结果返回至UI线程中,不需要通过(Thread操作和Handler操作). 使用时必须通 ...

  2. [Python3 填坑] 017 实例方法、静态方法、类方法的区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 先上例子 2.2 分析 1. print( 坑的信息 ) 挖坑时间:2019/04/07 明细 坑的编码 内容 Py024-1 实例方法 ...

  3. Java中数据类型的分类

    我们知道Java是强类型语言,那么肯定对应的也就有弱类型语言,以下介绍强类型语言与弱类型语言的区别: 强类型语言: 强类型语言也就是强制数据类型定义的语言.也就是说,一旦一个变量被指定了某个数据类型, ...

  4. 2018年牛客多校寒假 第四场 F (call to your teacher) (图的连通性)

    题目链接 传送门:https://ac.nowcoder.com/acm/contest/76/F 思路: 题目的意思就是判断图的连通性可以用可达性矩阵来求,至于图的存储可以用邻接矩阵来储存,求出来可 ...

  5. ES6——面向对象-基础

    面向对象原来写法 类和构造函数一样 属性和方法分开写的 // 老版本 function User(name, pass) { this.name = name this.pass = pass } U ...

  6. SIGINT、SIGQUIT、 SIGTERM、SIGSTOP区别

    2) SIGINT程序终止(interrupt)信号, 在用户键入INTR字符(通常是Ctrl-C)时发出,用于通知前台进程组终止进程. 3) SIGQUIT和SIGINT类似, 但由QUIT字符(通 ...

  7. Nginx优化_压缩处理与内存缓存

    对页面进行压缩处理; 服务器内存缓存. 1.对页面进行压缩处理 [root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf http { ... gzip ...

  8. 解决pycharm运行py文件时只有unittest选项的方法

    有时候在编完脚本开始运行时,发现某个py脚本右键运行的选项不是run,二是run in unittest,试过很多方法都不能很好的去除,主要是因为脚本中含有test字符串,一种解决方法是将脚本中所有的 ...

  9. CF9D How many trees? (dp)

    这题我想了好久 设 \(f_{i,j}\) 为 \(i\) 结点 \(<=j\) 的方案数 固定根,枚举左右子树,就有: \[f_{i,j}=\sum_{k=0}^{n-1}f_{k,j-1}* ...

  10. Python之批量读取文件【面试必学】

    python的os模块可以实现普遍的操作系统功能,并且和平台无关.以下为实现根目录下文件的批量读取. os.listdir(dirname)可以列出dirname下的目录和文件,依次读取相应的文件即可 ...