异常知识点一:
class ExceptionDemo2
{
/*调用了发生异常的函数前, 也要进行处理, 处理的方法之一就是抛出*/
public static void main(String[] args) throws NegativeIndexException
{
/*
final前面不能加static,方法之中的变量只是局部变量,并不能被方法之外所识别,所以并不能使用
public,static,private,protected...之内的修饰符。只有类才存在静态的变量 方法只能对静态变量
的操作 不能在方法内试图定义静态变量,否则的话会抛出编译错误。静态变量的本意是为搜索了让所
有的对象共享这个变量,如果在方法里面定义静态变量的话就存在逻辑错误了,也达不到你想要目的.
因为在方法中定义静态变量根本没有他的任何意义. 任何对象都有自己的方法,即使是静态方法,方法内
的变量也是在方法调用时候才开始分配内存,所以想给成静态的在逻辑上存在问题
*/
final int num = 3;
int [] arr = new int[num];
ExceptionDemo2 demo = new ExceptionDemo2();
demo.show(arr , -3);
System.out.println("hello world");
}
void show(int[] arr , int index) throws NegativeIndexException
{
System.out.println("step 1 ");
if(arr == null )
throw new NullPointerException("数组的引用不能为空");
if(index >= arr.length )
{
throw new ArrayIndexOutOfBoundsException("数组越界访问了!!!");
}
else if( index <= 0)
{
/*
ExceptionDemo2.java:36: 错误: 未报告的异常错误NegativeIndexException; 必须对其进
行捕获或声明以便抛出----- 抛出自定义异常之前, 应该声明一下自定义异常
*/
throw new NegativeIndexException("数组下标为负数");
}
else
System.out.println(arr[index]);
System.out.println("step 2 ");
}
}
/*
1. 我们可以自定义异常类,但是必须要继承异常体系,因为只有称为异常体系的子类才有资格具备可抛性,
才可以被两个关键字所操作 throw throws
2.
*/
class NegativeIndexException extends Exception
{
//定义构造函数
NegativeIndexException()
{
super();
}
NegativeIndexException(final String exceptionContents)
{
super(exceptionContents);
}
}
/*
1. System.out.println("hello world");和 System.out.println("step 2 ");都不执行了, 因为在System.out.println(arr[3]);
已经抛出异常给main函数了, System.out.println("step 1 ");要执行
2. 抛出的是一个exception异常对象;
3. throwable 可以抛出exception和error;
*/
异常知识点2:
class ExceptionDemo3
{
public static void main(String[] args)
{
ExceptionDemo3.show(-1);
System.out.println("Hello World!");
}
static void show( int index)
{
if(index < 0)
throw new NegativeDemo("下标为负数");
}
}
/*
异常的分类:
1. 编译时被检测异常:只要是Exception和其子类都是,除了特殊的子类RuntimeException体系
这种问题一旦出现,希望在编译时就进行检测,让这种问题有应对的方式,所以要有声明或者
处理办法---抛出
2. 编译时不检测异常(运行时异常):就是Exception中的RuntimeException和其子类
这种问题出现,无法让程序继续运行,这种问题一般不处理,直接编译通过, 在运行时,让
调用者调用是的程序强制停止,让调用者对代码进行修正;不需要声明或处理
throw 和throws的区别
throw是语句抛出一个异常。
语法:throw (异常对象);
throw e;
throws是方法可能抛出异常的声明。(用在声明方法时,表示该方法可能要抛出异常)
语法:[(修饰符)](返回值类型)(方法名)([参数列表])[throws(异常类)]{......}
public void doA(int a) throws Exception1,Exception3{......}
1、throws出现在方法函数头;而throw出现在函数体。
2、throws表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则
一定抛出了某种异常。
3、两者都是消极处理异常的方式(这里的消极并不是说这种方式不好),只是抛出或者可能抛出异常,
但是不会由函数去处理异常,真正的处理异常由函数的上层调用处理。
*/
class NegativeDemo extends RuntimeException
{
NegativeDemo()
{
super();
}
NegativeDemo(final String msg)
{
super(msg);
}
}
异常知识点3:
class ExceptionDemo4
{
public static void main(String[] args)
{
try
{
ExceptionDemo4.show(101);
System.out.println("try test");
}
catch (NegativeDemo e)
{
//继承了父类Throwable的getMessage方法
System.out.println("Exception Message : " + e.getMessage() );
System.out.println("String ---->"+ e ); //实际上调用的是e.toString()
}
catch(NullPointerException e)
{
System.out.println("String ---->"+ e ); //实际上调用的是e.toString()
}
catch(Exception e)
{
/*
一定要把这个catch 放到最后, 因为Exception是NegativeDemo和NullPointerException
的父类, 如果把这个catch放到最开头, 后面的catch都不会运行
*/
// System.exit(0); //推出jvm, 这时finally就不会执行
}
/* 通常用于关闭(释放)资源 */
finally
{
System.out.println("over!!!");
}
System.out.println("Hello World!");
}
static void show( int index) throws NegativeDemo,NullPointerException
{
if(index < 0)
throw new NegativeDemo("下标为负数");
if(index > 100)
throw new NullPointerException();
}
}
/*
异常处理的捕捉形式:
这是对异常进行的针对性处理格式
具体格式:
try
{
//需要被检测异常的代码
}
catch(异常类 变量)
{
//真正处理该异常的代码
}
finally
{
//一定会被执行的代码
}
*/
class NegativeDemo extends RuntimeException
{
NegativeDemo()
{
super();
}
NegativeDemo(final String msg)
{
super(msg);
}
}