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 ...
随机推荐
- python基础-11 socket,IO多路复用,select伪造多线程,select读写分离。socketserver源码分析
Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...
- jenkins无法显示html样式问题解决
利用jenkins的以下两个插件可以巧妙解决这个问题 Startup Trigger: 可实现在Jenkins节点(master/slave)启动时触发构建: Groovy plugin: 可实现直接 ...
- MD5加密 和 自定义加密解密
public class EncryptString { /// <summary> /// MD5加密 /// </summary> /// <param name=& ...
- 旧接口注册LED字符驱动设备(动态映射)
#include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...
- JS补充笔记
<script> 函数: 普通函数: function func(){ } 匿名函数: setInterval("func()",5000); setInterval( ...
- EF添加关联的提示问题:映射从第 260 行开始的片段时有问题:
一,EF添加关联的提示问题 严重性 代码 说明 项目 文件 行 禁止显示状态错误 错误 3004: 映射从第 260 行开始的片段时有问题:没有为 设置 T_xx_xxRelation 中的属性 T_ ...
- Linux忘记root密码解决方案
忘记Linux root密码时,只需重启Linux系统,然后引导进入Linux的单用户模式(init 1),由于单用户模式不需要输入登陆密码,因此,可直接登陆系统,修改root密码即可解决问题.需要说 ...
- 多线程AQS
参考: AQS原理分析 https://blog.csdn.net/javazejian/article/details/75043422 重入读写锁原理分析 https://blog.csdn.ne ...
- thinkphp url和路由
一.入口模块修改 修改public下的index 加入 define('BIND_MODULE','admin'); 即可将入门模块绑定到admin模块 <?php // [ 应用入口文件 ] ...
- 在 CentOS 上部署 GitLab (自托管的Git项目仓库)
参考资料https://github.com/mattias-ohlsson/gitlab-installer/blob/master/gitlab-install-el6.sh 环境准备OS: Ce ...