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的集线器 前台在调用的时候出现了问题(经检查是代理对象 ...
随机推荐
- ubuntu20.04开机自动运行脚本实例
在 Ubuntu 20.04 中,/etc/rc.local 文件仍然存在,但不再默认启用,因为它已经被 systemd 代替.下面是使用systemd开机执行的脚本的实例: 1.编写脚本myscri ...
- raster2pgsql 执行命令
raster2pgsql -s 4326 -I -C -M /home/radar_202210251000.tif public.radar_data_xx | psql -h 120.46.210 ...
- php对接钉钉机器人报警接口
<?php function request_by_curl($remote_server, $post_string) { $ch = curl_init(); curl_setopt($ch ...
- SOAMANAGER 500 ERROR
.调查原因是 SICF的服务有一个被开启了,取消激活就能解决这个问题. 可能的原因是在激活fiori的时候会自动激活
- [转载]OpenCV中的channel是什么意思?
转载自https://answers.opencv.org/question/7585/meaning-of-channels/ 简单来说,就是描述一个pixel的颜色用多少个独立的参数描述,这个个数 ...
- 230219 Business 31-48
31. 31: Maternity LeaveVeronica, when is your baby due?Next month.Are you going on maternity leave? ...
- debian11用iso制作本地apt源
摘抄记录,原文链接: https://blog.csdn.net/leejearl/article/details/122708953?spm=1001.2101.3001.6650.1&ut ...
- Unity 动态加载图片出现红色问号
因为图片必须是RGB格式的才可以 ,Cao!!!
- 生产环境自动备份win服务器所有web项目(IIS+项目代码)
@echo offrem 功能:每月自动备份本服务器所有web项目rem 日期:2022.3.10rem 制作人:zl rem 定义变量Y为备份时间:YYYYMMset y=%date:~0,4%%d ...
- 推测执行 Speculative execution
如果我们只是靠随便网络上查找一个Speculative这个词的含义,是很难去理解的.但是我们通过查看英文原文去理解,可能就比较清楚地理解了: speculative (adjective) 1. ba ...