【java】一文带你了解异常处理
·
异常
异常分为两种,一种编译时异常,是一定要进行处理的,不然编译都不会过,一种是运行时异常,比如索引越界,算术异常,空指针异常等,这种在编译的时候不会报错,而是在运行到这行代码才会报错。
一种是在函数内部进行处理,用try-catch或者try-catch-finally来处理。
处理异常的方法也有两种,一种是在函数声明的时候抛出,然后由函数调用者进行异常处理。
try-catch-finally
public class Exception {
public static void main(String[] args) {
try
{
int result=divide(4,0);
System.out.println(result);
}
catch(ArithmeticException e)
{
System.out.println("算术异常:"+e.getMessage());
//System.exit(0);
}
finally {
System.out.println("除了jvm关闭以外一定会处理的finally");
}
System.out.println("try catch 外");
}
public static int divide(int x,int y)
{
int result=x/y;
return result;
}
}
运行结果
无System.exit(0):
算术异常:/ by zero
除了jvm关闭以外一定会处理的finally
try catch 外
有 System.exit(0);
算术异常:/ by zero
函数名声明时抛出
在函数声明抛出,没有进行处理,则程序在检测到异常的时候直接抛出然后不进行后面代码
public class Exception {
public static void main(String[] args) {
int result =divide(4,0);
System.out.println(result);
System.out.println("divide 后的代码");
}
public static int divide(int x,int y) throws ArithmeticException
{
int result=x/y;
return result;
}
}
//结果
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Exception.divide(Exception.java:23)
at Exception.main(Exception.java:17)
在函数声明抛出,在调用处处理,程序在抛出异常后仍然会进行下面的代码
public class Exception {
public static void main(String[] args) {
try{
int result =divide(4,0);
System.out.println(result);
System.out.println("divide 后的代码");
}
catch (ArithmeticException e){
System.out.println("异常:"+e.getMessage());
}
System.out.println("try-catch后面的代码");
}
public static int divide(int x,int y) throws ArithmeticException
{
int result=x/y;
return result;
}
}
//结果
异常:/ by zero
try-catch后面的代码
自定义异常类
class DivideByMinusException extends Exception {
//无参构造
public DivideByMinusException() {
super();
}
//有参构造
public DivideByMinusException(String message) {
super(message);
}
}
public class myException {
public static void main(String[] args) {
int result=0;
try
{
result=divide(4,-1);
System.out.println(result);
}
catch (DivideByMinusException e)
{
System.out.println(e.getMessage());
}
}
//抛出异常
public static int divide(int x,int y) throws DivideByMinusException
{
if(y<0)
{
//自定义异常信息
//这个自定义异常信息就是构造一个新对象把值传进去,只要传递对的参数进去就行。
throw new DivideByMinusException("除数是负数");
}
else if(y==0)
{
throw new DivideByMinusException("除数是零");
}
return x/y;
}
}
结果
//result=divide(4,-1)
除数是负数
//result=divide(4,0);
除数是零
//result=divide(4,1);
4
关于自定义对象的输出信息
来自菜鸟教程
//自定义异常类,继承Exception类
public class InsufficientFundsException extends Exception
{
//此处的amount用来储存当出现异常(取出钱多于余额时)所缺乏的钱
private double amount;
//need传递到这里来(need在下面异常处理里)
public InsufficientFundsException(double amount)
{
this.amount = amount;
}
//使用提示信息的时候就用e.getAmount来获取需要的金额
public double getAmount()
{
return amount;
}
}
// 文件名称 CheckingAccount.java
import java.io.*;
//此类模拟银行账户
public class CheckingAccount
{
//balance为余额,number为卡号
private double balance;
private int number;
public CheckingAccount(int number)
{
this.number = number;
}
//方法:存钱
public void deposit(double amount)
{
balance += amount;
}
//方法:取钱
public void withdraw(double amount) throws InsufficientFundsException
{
if(amount <= balance)
{
balance -= amount;
}
else
{
double needs = amount - balance;
//这里的need传进去创建对象
throw new InsufficientFundsException(needs);
}
}
//方法:返回余额
public double getBalance()
{
return balance;
}
//方法:返回卡号
public int getNumber()
{
return number;
}
}
更多推荐




所有评论(0)