04异常处理

课堂测验

 

 

 

 

 

 源代码:

package shiyan;
import javax.swing.*;
class Test{

public static void main(String[] args)
{
int i=1, j=0, k;
k=i/j;


try
{

k = i/j; // Causes division-by-zero exception
//throw new Exception("Hello.Exception!");
}

catch ( ArithmeticException e)
{
System.out.println("被0除. "+ e.getMessage());
}

catch (Exception e)
{
if (e instanceof ArithmeticException)
System.out.println("被0除");
else
{
System.out.println(e.getMessage());

}
}


finally
{
JOptionPane.showConfirmDialog(null,"OK");
}

}
}

结果:

 

 异常对象的两个来源:1.Java运行时环境自动抛出系统生成的异常,而不管你是否愿意捕获和处理,它总要被抛出。比如除数为0的异常。2.程序员自己抛出的异常,这个异常可以是程序员自己定义的,也可以是Java语言中定义的,用throw关键字抛出异常,这种异常常常用来向调用者汇报异常的一些信息。

异常是针对方法来说的,抛出、声明抛出、捕获和处理异常都是在方法中进行的。

Java异常处理铜通过5个关键字try、catch、throw、throws、finally进行管理。基本过程是用try语句块包住要监视的语句,如果语句块内出现异常,则异常会被抛出你的代码在catch语句块中可以捕获到这个异常并做处理;还有一部分系统生成的异常在Java运行时自动抛出。你也可以通过throws关键字在方法上声明该方法要抛出异常,然后再方法内部通过throw抛出异常对象。finally语句块会在方法执行热天润之前执行,一般结构如下:

try{

代码

}catch(异常类型1异常变量名1)

代码

}catch(异常类型2异常变量名2){

代码

}finally{

代码

}

java.lang.ArithmeticException

代码:

package test;

public class yichangchuli {
    public static void main(String[] args) {
    int i=1,j=0,a;
    a=i/j;
    System.out.println(a);
    }
}

这个异常的解释是“数学运算异常”。比如程序中出现了除以零这样的运算就会出这样的异常,对这种异常,要好好检查一下自己程序中涉及到数学运算的地方,看有没有哪一方面写的不符合数学的计算规则。

关于“finally”的作用

 

 

动手动脑:

代码:

public class CatchWho {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}

throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}

 

 代码:

public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}

 

 

 

 代码:


public class EmbededFinally {


public static void main(String args[]) {

int result;

try {

System.out.println("in Level 1");


try {

System.out.println("in Level 2");
// result=100/0; //Level 2

try {

System.out.println("in Level 3");

result=100/0; //Level 3

}

catch (Exception e) {

System.out.println("Level 3:" + e.getClass().toString());

}


finally {

System.out.println("In Level 3 finally");

}


// result=100/0; //Level 2


}

catch (Exception e) {

System.out.println("Level 2:" + e.getClass().toString());

}
finally {

System.out.println("In Level 2 finally");

}

// result = 100 / 0; //level 1

}

catch (Exception e) {

System.out.println("Level 1:" + e.getClass().toString());

}

finally {

. System.out.println("In Level 1 finally");

}

}

}

 

 

总结:Java通过使用try…catch…finally语句来捕获一个或多个异常,catch语句可以有一个或多个,而且至少要一个catch语句或finally。如果某处发生异常,则try语句中此处之后的代码都不会被执行,如:

Try{ 

String s=”abc”;

int i=Integer.parseInt(s);

int a=i+10;

}

当程序运行时,异常发生,则try语句抛出异常对象,a=i+10;此语句位于异常发生处之后,不会被执行。

Finally语句用法:由于当异常发生时,已经分配的资源会保持原来的状态,不能被释放,所以finally语句主要用来释放和清理有关的资源或善后工作,如关闭打开的文件等。Finally语句是一个统一的出口,即无论try语句中是否发生异常,也无论catch语句的的异常类型与try语句抛出的异常的类型是否匹配,finally语句中的代码都要被执行。Finally语句不是必需的,若在某些场合中不需要释放资源,那么try…catch语句中可以没有finally语句。

 

finally语句是一定会执行吗?
代码:

public class SystemExitAndFinally {


public static void main(String[] args)
{

try{


System.out.println("in main");

throw new Exception("Exception is thrown in main");

//System.exit(0);


}

catch(Exception e)

{

System.out.println(e.getMessage());

System.exit(0);

}

finally

{

System.out.println("in finally");

}

}


}

 

 并没有执行finally语句,说明finally语句并不是一定会执行的。

关于throws语句:

 

 

 

 

 

posted @   早起早起^^  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示