异常处理try...catch
Posted 简简单单zjl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了异常处理try...catch相关的知识,希望对你有一定的参考价值。
package com.day16.Exception;
/*
* 异常处理的两种方式
* 1.try...catch...finally
* *try catch
* *try catch finally
* *try finally
* 2.throws
* try:用来检测异常的
* catch:用来捕获异常的
* finally:释放资源
*/
public class ExceptionOne {
public static void main(String[] args) {
Demo d=new Demo();
try {
int x=d.div(10,0);
System.out.println(x);
}catch(ArithmeticException a) {//相当于ArithmeticException a=new ArithmeticException();
System.out.println("除数为0了");//除数为0了
}
System.out.println("Kobe");//Kobe
}
}
class Demo{
public int div(int a,int b) {
return a/b;
}
}
以上是关于异常处理try...catch的主要内容,如果未能解决你的问题,请参考以下文章
Java异常处理机制:try...catch...的执行流程
水能详细讲解一下java 中的异常处理 try catch throw