Java常用工具——java异常
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异常的更多相关文章
- Java常用工具——java字符串
一.String常用字符串 package com.imooc.string; public class StringDemo { public static void main(String[] a ...
- Java常用工具——java集合
一.ArrayList package com.imooc.set; import java.util.ArrayList; import java.util.List; public class A ...
- Java常用工具——java多线程
一.线程的创建 方式一:继承Thread类,重写run()方法 package com.imooc.thread1; class MyThread extends Thread{ public MyT ...
- Java常用工具——java包装类
一.包装类和基本数据类型 装箱:基本数据类型——包装类 拆箱:包装类——基本数据类型 package com.imooc.wrap; public class WrapTestOne { public ...
- JavaEE-实验一 Java常用工具类编程
该博客仅专为我的小伙伴提供参考而附加,没空加上代码具体解析,望各位谅解 1. 使用类String类的分割split 将字符串 “Solutions to selected exercises ca ...
- Java常用工具+类库合集
1 常用工具 JVisual vm:可以直接通过软件包下载,支持本地以及远程JVM监控 JMH:Java Microbenchmark Harness,测试基准组件,精度可达纳秒级 JITWatch: ...
- Java常用工具类题库
一. 填空题 在Java中每个Java基本类型在java.lang包中都在一个相应的包装类,把基本类型数据转换为对象,其中包装类Integer是___Number__的直接子类. 包装类Inte ...
- JAVA(三)JAVA常用类库/JAVA IO
成鹏致远 | lcw.cnblog.com |2014-02-01 JAVA常用类库 1.StringBuffer StringBuffer是使用缓冲区的,本身也是操作字符串的,但是与String类不 ...
- JAVA常用工具类汇总
一.功能方法目录清单: 1.getString(String sSource)的功能是判断参数是否为空,为空返回"",否则返回其值: 2.getString(int iSource ...
随机推荐
- Vue 基础 day06 webpack 3.x 结合vue
在普通页面使用 render 函数渲染组件 var login = { template: '<h3>login</h3>' } var vm = new Vue({ // c ...
- [Luogu 4688] [Ynoi2016]掉进兔子洞 (莫队+bitset)
[Luogu 4688] [Ynoi2016]掉进兔子洞 (莫队+bitset) 题面 一个长为 n 的序列 a.有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间 ...
- [常用类]Math、Random、System、BigInteger、BigDecimal
Math类中的成员全是静态成员,构造方法是 私有的,以避免被创建对象 常用方法: int abs() double ceil() //向上取整 double floor() //向下取整 int ma ...
- Mock接口数据 = mock服务 + iptable配置
一.mock接口数据应用场景: 1.测试接口A,A接口代码中调用其他服务的B接口,由于开发排期.测试环境不通等原因,依赖接口不可用 2.测试异常情况,依赖接口B返回的数据格式不对.返回None.超时等 ...
- LeetCode Lect7 堆及其应用
概述 堆是一颗完全二叉树.分为大根堆(父节点>=所有的子节点)和小根堆(父节点<=所有的子节点). 插入.删除堆顶都是O(logN),查询最值是O(1). 完全二叉树(Complete B ...
- CSS的重用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- $_ENV输出为null的原因及解决办法
有些朋友输出$_ENV是空的,可能原因是php.ini的variables_order值为"GPCS",也就是说系统在定义PHP预定义变量时的顺序是GET,POST,COOKIES ...
- Vue简单封装axios—解决post请求后端接收不到参数问题
1.在src/下新建api文件夹,api/下新建index.js和public.js 在public.js中: import axios from 'axios'; import qs from 'q ...
- 2019-8-31-dotnet-core-集成到-Mattermost-聊天工具
title author date CreateTime categories dotnet core 集成到 Mattermost 聊天工具 lindexi 2019-08-31 16:55:58 ...
- CF9D How many trees? (dp)
这题我想了好久 设 \(f_{i,j}\) 为 \(i\) 结点 \(<=j\) 的方案数 固定根,枚举左右子树,就有: \[f_{i,j}=\sum_{k=0}^{n-1}f_{k,j-1}* ...