我在乐字节学习的第十天(Java)

今天是我在乐字节学习的第十天啦,老师讲的是【异常处理机制】

今天老师讲了异常处理,咱们可以一起来探讨下哈!如果有哪里不对的地方可以指出来哦!

运行结果:为了更好的理解异常机制,里面输出了一些多余的话。勿喷

下面是源码:欢迎老司机试开

class FuShuIndexException extends Exception

{

FuShuIndexException(){}

FuShuIndexException(String s)

{

super(s);

System.out.println("这是FuShuIndexException类的内容。。。。");

}

}

class Demo

{

public int method(int[] arr,int Index)throws FuShuIndexException

{

if (Index<0)

{

throw new FuShuIndexException("数组角标为负啦!!抛出异常。");

}

return arr[Index];

}

}

public class ExceptionDemo

{

public static void main(String[] args)//throws FuShuIndexException

{

int[] arr=new int[3];

//int num=arr[-3];

Demo d=new Demo();

try

{

int x=d.method(arr,-10);

System.out.println(x);

}

catch(FuShuIndexException e)

{

System.out.println("已捕获异常。。。catch");

System.out.println("print getMessage:"+e.getMessage());

System.out.println("print e:"+e);

e.printStackTrace();//jvm默认的异常处理机制调用的就是这个方法

System.out.println("角标为负异常,catch语句输出。");

}

//System.out.println(num);

System.out.println("over");

}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容