欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java中如何能知道应该捕获什么样的异常?举例

程序员文章站 2023-11-07 22:24:52
我怎么知道应该捕获什么样的异常? (视频下载) (全部书籍)马克-to-win:如上例1.1:开始没加try时,程序崩溃,系统打印的是如下的错误,Exception in thread "main" java.lang.ArithmeticException: / by zero at Test.m ......

我怎么知道应该捕获什么样的异常?  马克-to-win:如上例1.1:开始没加try时,程序崩溃,系统打印的是如下的错误,exception in thread "main" java.lang.arithmeticexception: / by zero at test.main(test.java:4),马克-to-win: 所以我们就该捕获arithmeticexception.见下例:1.1.3。

例:1.1.3-本章源码

public class test {
    public static void main(string[] args) {
        try
        {
            int userinput=0;
            int i = 6 / userinput;
        }
        catch(arithmeticexception e)
        {
            system.out.println(e);
        }
。。。。。。。。。。。。。。。。。
详情请进:http://www.mark-to-win.com/index.html?content=javabeginner/javaurl.html&chapter=javabeginner/javabeginner5_web.html#whattocatch