异常机制

一.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-异常的更多相关文章

  1. Day16异常1

    package com.exception.demo01;public class demo01 { public static void main(String[] args) { try{new ...

  2. 基本数据类型 异常 数组排序 JVM区域划分

               Day01 1.基本数据类型各占几个字节 Byte 1 short2 int4 long8 float4 double6 char2 boolean1 Byte b1=3,b2= ...

  3. SSM-SpringMVC-24:SpringMVC异常高级之自定义异常

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 自定义异常,大家都会,对吧,无非就是继承异常类等操作,很简单,我就不多扯皮了,但是在xml配置文件中有个不同的 ...

  4. python学习-Day16

    目录 今日内容详细 内置函数补充 常见内置函数 help() id() int() isinstance() pow() round() sum() 求和 迭代器 可迭代对象 什么是可迭代对象? 哪些 ...

  5. day16 异常处理生成器

    day16 异常处理生成器 今日内容概要 异常处理 异常处理实战应用 生成器对象 生成器对象实现range方法 生成器表达式 今日内容详细 一.异常处理 1.异常常见类型 SyntaxError语法错 ...

  6. alias导致virtualenv异常的分析和解法

    title: alias导致virtualenv异常的分析和解法 toc: true comments: true date: 2016-06-27 23:40:56 tags: [OS X, ZSH ...

  7. ASP.NET Core应用的错误处理[2]:DeveloperExceptionPageMiddleware中间件如何呈现“开发者异常页面”

    在<ASP.NET Core应用的错误处理[1]:三种呈现错误页面的方式>中,我们通过几个简单的实例演示了如何呈现一个错误页面,这些错误页面的呈现分别由三个对应的中间件来完成,接下来我们将 ...

  8. 记一次tomcat线程创建异常调优:unable to create new native thread

    测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...

  9. 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法

    在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常.   意思是出现了死循环,也就是Model之间有循环包含关系: ...

  10. SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论

    异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#signalR 后台创建了一个DntHub的集线器 前台在调用的时候出现了问题(经检查是代理对象 ...

随机推荐

  1. Cendos中启动docker失败的问题

    注:在 Windows中使用Linux虚拟机中使用Cendos 刚开始安装Docker时没有任何错误, 但是在后续的docker启动过程中, 出现以下问题: 按提示使用 systemctl statu ...

  2. Python学习的第一次总结

    执行Python的方式:1.交互器(不易保存,闪现,不方便看)2.用win-cmd 来执行(用notepad++保存后执行) 开始 >> cmd >> cd c:\ #切c盘& ...

  3. TP开发项目时遇到的问题记录

    1.下载功能. TP自带Http下载类,使用时new一个就行,示例代码: public function download(){ //接收公文id $id = I('get.fid'); //根据公文 ...

  4. uni-app --vue3--TypeScript 微信小程序开发

    微信小程序开发文档: https://developers.weixin.qq.com/miniprogram/dev/framework/quickstart/getstart.html#%E7%9 ...

  5. 国产低功耗Soc蓝牙语音遥控器芯片HS6621 指纹锁、体脂称等应用方案

    随着物联网技术不断发展,家用电器往智能化方向持续迭代,使用红外遥控器这种传统的互动方式已经满足不了实际的使用需求,蓝牙语音遥控器作为人机交互新载体,逐渐取代传统红外遥控器成为家居设备的标配.相比于传统 ...

  6. ref(代替id)

    App.vue <template> <div> <Student ref="str"/> <h3 v-text="age&qu ...

  7. golang 实现twitter雪花算法

    1 /* 2 * twitter雪花算法golang实现,生成唯一趋势自增id 3 * 保留位:63位 4 * 毫秒时间戳:[62-20]43位,时间范围[1970-01-01 00:00:00.00 ...

  8. 使用shell定时执行脚本

    yum install crontabs //安装 #检查安装crontab -V #服务操作service crond start //启动服务service crond stop //关闭服务se ...

  9. nginx日志按日期存储

    http {     include       mime.types;     default_type  application/octet-stream;     map $time_iso86 ...

  10. hyperfine spectra

    !Hyperfine spectradefine int n xlet xlet name spect-'x'!file in 'name'.basfindget 3set mod x aset un ...