码迷,mamicode.com
首页 > 编程语言 > 详细

java 异常

时间:2017-12-21 01:48:30      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:system   end   子类   必须   throwable   compile   声明   base   cheng   

异常体系:
---| Throwable 所有错误或者异常的父类
------| Error(错误)
------| Exception(异常) 异常一般都通过代码处理
--------| 运行时异常: 如果一个方法内部抛出了一个运行时异常,那么方法可以忽略这个异常,即可以 不处理,调用者也可以不处理,编译都能通过
--------| 编译时异常: 如果一个方法内部抛出了一个编译时异常,那么方法上就必须要 捕获或抛出处理,如果是抛出处理,调用者也必须要处理,否则编译无法通过



package com.czbk33.base.day11;

/**
* Created by chengtao on 17/10/18.
*

异常体系:
--------| Throwable 所有错误或者异常的父类
--------------| Error(错误)
--------------| Exception(异常) 异常一般都通过代码处理
------------------| 运行时异常: 如果一个方法内部抛出了一个运行时异常,那么方法可以忽略这个异常,即可以 不处理,调用者也可以不处理。
------------------| 编译时异常: 如果一个方法内部抛出了一个编译时异常,那么方法上就必须要 捕获或抛出处理,如果是抛出处理,调用者也必须要处理。

运行时异常: RuntimeException以及RuntimeException子类 都是属于运行时异常。
编译时异常: 除了运行时异常就是编译异常。
*
*
*
*
* 异常体系:Throwable 所有错误或者异常的父类Error(错误)Exception(异常) 异常一般都通过代码处理.
运行时异常: 如果一个方法内部抛出了一个运行时异常,那么方法上 可以声明也可以不 声明,调用者可以以处理也可以不处理。
编译时异常(非运行时异常、受检异常): 如果一个方法内部抛出了一个编译时异常对象,
要么 在方法内 捕获处理
要么 在方法上声明抛出,做抛出处理,而且调用者也必须要处理。
运行时异常: RuntimeException以及RuntimeException子类 都是属于运行时异常。

编译时异常: 除了运行时异常就是编译异常。
区别:在编译时,是否检查 对该异常进行了处(捕获/抛出 处理均可)

*/
public class TestTwoDifferntException {
public static void main(String[] args) {
TestTwoDifferntException entity = new TestTwoDifferntException();
System.out.println("--------- 编译时异常 -----------");
try {
entity.methodComplie02();
}catch (MyCompileException e){
System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
}

System.out.println("--------- 运行时异常 -----------");
try {
entity.methodRun01();
}catch (MyRunTimeException e){
System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
}

try {
entity.methodRun02();
}catch (MyRunTimeException e){
System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
}

try {
entity.methodRun03();
}catch (MyRunTimeException e){
System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
}

try {
entity.methodRun04();
}catch (MyRunTimeException e){
System.out.println("main 方法中 捕获的到异常信息 code = " + e.getCode() + ", message = " + e.getMessage());
}
}

public void methodComplie00(int a ){
if(a==0){
// throw new MyCompileException("编译时异常",1);
}
}

//1.编译时异常,必须处理(捕获/抛出 均可)
public void methodComplie01(){
// throw new MyCompileException("编译时异常",1);
}

//2.抛出 处理 编译时异常
public void methodComplie02() throws MyCompileException{
throw new MyCompileException("编译时异常",2);
}

//3.捕获 处理 编译时异常
public void methodComplie03(){
try {
throw new MyCompileException("编译时异常", 3);
}catch (Exception e){
System.out.println("捕获 处理 编译时异常");
}
}


//4.捕获 处理 编译时异常
public void methodComplie04(){
try {
throw new MyCompileException("编译时异常", 4);
}catch (MyCompileException e){
System.out.println("捕获到的异常信息:--> " +"code = " + e.getCode() + ", message = " + e.getMessage());
// throw e;
}
}


//1.运行时异常,可以不做 处理(捕获/抛出 都不做)
public void methodRun01(){
throw new MyRunTimeException("运行时异常",1);
}

//2.抛出 处理 编译时异常
public void methodRun02()throws MyRunTimeException{
throw new MyRunTimeException("运行时异常",2);
}

//3.捕获 处理 编译时异常
public void methodRun03(){
try {
throw new MyRunTimeException("运行时异常",3);
}catch (MyRunTimeException e ){
System.out.println("捕获到的异常信息:--> " +"code = " + e.getCode() + ", message = " + e.getMessage());
}
}

//4.捕获 处理 编译时异常
public void methodRun04(){
try {
throw new MyRunTimeException("运行时异常",4);
}catch (MyRunTimeException e){
System.out.println("捕获到的异常信息:--> " +"code = " + e.getCode() + ", message = " + e.getMessage());
throw e;
}
}
}
 

--------------------------------

--------------------------------

 




package com.czbk33.base.day11;

/**
* Created by chengtao on 17/10/17.
* InterruptedException 是一个编译时的异常
*/
public class MyCompileException extends InterruptedException {
private String message;
private int code;

public MyCompileException() {
}

public MyCompileException(String message, int code) {
this.message = message;
this.code = code;
}

@Override
public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}
}



--------------------------------
--------------------------------


package com.czbk33.base.day11;

/**
* Created by chengtao on 17/10/17.
*/
public class MyRunTimeException extends RuntimeException {
private String message;
private int code;

public MyRunTimeException() {
}

public MyRunTimeException(String message, int code) {
this.message = message;
this.code = code;
}

@Override
public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}
}
 

java 异常

标签:system   end   子类   必须   throwable   compile   声明   base   cheng   

原文地址:http://www.cnblogs.com/ctaixw/p/8076432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!