Day16-异常
异常机制
一.Error和Exception
1.什么是异常

2.异常体系结构

3.Error和Exception
Error

Exception

二.捕获和抛出异常
1.异常处理机制
抛出异常
捕获异常
异常处理五个关键字:
try,catch.finally,throw,throws
例1:捕获Exception
package exception;
public class Test {
public static void main(String[] args) {
int a=1;
int b=0;
try {//try监控区域
System.out.println(a/b);
}catch (ArithmeticException e){//catch捕获异常
System.out.println("程序出现异常,变量b不能为0");
}finally {//处理善后工作
System.out.println("finally");
}
//finally 可以不要finally。假设IO,资源,关闭
}
}
例2:捕获Error
package exception;
public class Test {
public static void main(String[] args) {
int a=1;
int b=0;
try {//try监控区域
new Test().a();
}catch (Error e){//这里用Throwable也可以//catch(想要捕获的异常类型)捕获异常
System.out.println("成功捕获Error");
}finally {//处理善后工作
System.out.println("finally");
}
//finally 可以不要finally。假设IO,资源,关闭
}
public void a(){
b();
}
public void b(){
a();
}
}
例3:假设捕获多个异常
package exception;
public class Test {
public static void main(String[] args) {
int a=1;
int b=0;
//假设捕获多个异常,从小到大
try {//try监控区域
System.out.println(a/b);
}catch (Error e){//这里用Throwable也可以//catch(想要捕获的异常类型)捕获异常
System.out.println("成功捕获Error");
}catch (Exception e){
System.out.println("成功捕获Exception");
}catch (Throwable e){
System.out.println("成功捕获Throwable");
} finally {//处理善后工作
System.out.println("finally");
}
//finally 可以不要finally。假设IO,资源,关闭
}
public void a(){
b();
}
public void b(){
a();
}
}
例4:主动抛出异常
package exception;
public class test2 {
public static void main(String[] args) {
try {
new test2().test(1,0);
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
//假设这方法中,吃力不了这个异常,方法上抛出异常
public void test(int a,int b)throws ArithmeticException{
if (b == 0) {// throw throws
throw new ArithmeticException();//主动抛出异常,一般在方法中使用
}
}
}
三.自定义异常

1.自定义异常类
package MyException;
//自定义的异常类
public class myexception extends Exception{
//传递数字>10
private int detail;
public myexception(int a) {
this.detail = a;
}
//to string:异常的打印信息
@Override
public String toString() {
return "myexception{" + "detail=" + detail + '}';
}
}
2.测试类
package MyException;
public class test {
//可能会存在异常的方法
static void test(int a) throws myexception {
System.out.println("传递的参数为:"+a);
if (a>10){
throw new myexception(a);//抛出
}
System.out.println("OK");
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException.myexception e) {
System.out.println("my exception-->"+e);
}
}
}
//运行结果
//传递的参数为:11
//my exception-->myexception{detail=11}
实际应用中的经验总结

Day16-异常的更多相关文章
- Day16异常1
package com.exception.demo01;public class demo01 { public static void main(String[] args) { try{new ...
- 基本数据类型 异常 数组排序 JVM区域划分
Day01 1.基本数据类型各占几个字节 Byte 1 short2 int4 long8 float4 double6 char2 boolean1 Byte b1=3,b2= ...
- SSM-SpringMVC-24:SpringMVC异常高级之自定义异常
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 自定义异常,大家都会,对吧,无非就是继承异常类等操作,很简单,我就不多扯皮了,但是在xml配置文件中有个不同的 ...
- python学习-Day16
目录 今日内容详细 内置函数补充 常见内置函数 help() id() int() isinstance() pow() round() sum() 求和 迭代器 可迭代对象 什么是可迭代对象? 哪些 ...
- day16 异常处理生成器
day16 异常处理生成器 今日内容概要 异常处理 异常处理实战应用 生成器对象 生成器对象实现range方法 生成器表达式 今日内容详细 一.异常处理 1.异常常见类型 SyntaxError语法错 ...
- alias导致virtualenv异常的分析和解法
title: alias导致virtualenv异常的分析和解法 toc: true comments: true date: 2016-06-27 23:40:56 tags: [OS X, ZSH ...
- ASP.NET Core应用的错误处理[2]:DeveloperExceptionPageMiddleware中间件如何呈现“开发者异常页面”
在<ASP.NET Core应用的错误处理[1]:三种呈现错误页面的方式>中,我们通过几个简单的实例演示了如何呈现一个错误页面,这些错误页面的呈现分别由三个对应的中间件来完成,接下来我们将 ...
- 记一次tomcat线程创建异常调优:unable to create new native thread
测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...
- 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常. 意思是出现了死循环,也就是Model之间有循环包含关系: ...
- SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#signalR 后台创建了一个DntHub的集线器 前台在调用的时候出现了问题(经检查是代理对象 ...
随机推荐
- 记录安装perl-Verilog过程
开始,编译带Verilog::Netlist的脚本,报 YumRepo Error: All mirror URLs are not using ftp, http[s] or file.centos ...
- 快速上手springboot(2)
简介 Spring程序的缺点: 1.依赖设置繁琐 2.配置繁琐 SpringBoot程序优点 1.起步以来(简化依赖配置) 2.自动配置(简化常用工程相关配置) 3.辅助功能(内置服务器,...) S ...
- 【当年笔记】Collection集合部分
集合继承关系图 1)Vector 特点:线程安全,消耗偏大 2)ArrayList 特点:基于数组实现,随机访问某个元素效率高.集和头尾之间包括头插入删除操作效率较低,因为插入元素后,其他元素要后移. ...
- 【服务器数据恢复】RAID6数据恢复案例
服务器数据恢复环境:一台Web服务器中有一组由8块磁盘组建的raid6磁盘阵列,用来运行数据库和存储普通办公文件. 服务器故障:服务器raid6磁盘阵列中有两块硬盘离线,但是管理员没有注意到这种情况, ...
- android系统签名文件路径及签名方法
在系统源码路径下 签名文件路径:android/build/target/product/security/ 准备好签名工具:"signapk.jar" 位置:android/pr ...
- WPF materialDesign 锁屏后界面卡死问题解决
materialDesign的一个缓存机制问题,在xaml文件中Window属性添加一行: materialDesign:ShadowAssist.CacheMode="{x:Null}&q ...
- 数据库常用sql
1.创建表 create table 表名( 字段名 类型 约束, 字段名 类型 约束 ... ) 如:create table students( id int unsigned primary k ...
- 实用的JavaScript技巧
1.数组去重 let arr = [...new Set([1,2,3,2,1])]; //输出:[1, 2, 3] 2.删除数组中的虚值(undefined .null.NaN.0 .'' .fal ...
- Spring Boot应用启动
1.Eclipse 中启动Spring Boot应用 右键应用程序启动类, Run As Java Application 2.maven 命令: mvn spring-boot:run 在应用程序启 ...
- 搭建ftp服务器的超详细步骤
第一步:打开控制面板. 1.1选择程序这个选项. 1.2选择启用或关闭window功能 1.3勾选如图有红箭头的这几个选项. 第二步:搜索iis且将其打开 . 2.1点击网站,且点击添加网站 物理路径 ...