JAVA 异常 throwable exception error throws throw

1.如何理解Exception,Error和Throwable
    Throwable是Exception和Error的父类.
    Error表示错误,一般是系统级的错误!
     Exception一般是程序运行期间的错误!
   
    通常在使用  try{}catch(Exception e){} 这种结构的时候,只能找到一半的错误,也就是说只能捕获Exception范围内的异常 并处理使得程序能够正常运行.而Error范围内的错误就无法捕获并处理.

   通过 try{}catch(Throwable a){} 的方式能够处理,但是一般情况下不这样做。
   原因在于查看Error下面的子类,VirtualMachineError,ThreadDeath,LinkageError,从名字上看出来这些错误都是非常非常严重的,到底是否需要去捕获或者处理呢?
  
    Error的产生一般是JVM或者是操作系统的问题,JAVA 文档中对Error的说明是:"Error是Throwable的子类,它的出现说明出现了严重的问题。一般应用程序除非有理由,否则不应该捕捉Error,通常这是非常反常的情况".

    Exception的产生主要是在程序运行期间发生的一些不正常事件中止了程序的运行,可以通过JAVA异常处理机制捕获异常并处理,使得程序正常运行下去。这些异常(不正常事件)有别于Error错误,它们通常是可修复的,程序员可以处理的。

2.运行时异常和受检查异常
    1)运行时异常,属于RuntimeException类及子类范围的类(以及衍生类)都属于运行时异常。
     2)受检查异常,在Exception范围内,除了运行时异常的类都是受检查异常类,为checked exception
    3)它们之间的区别在于: 例如在代码中写了 throw new Exception(); 和 throw new RuntimeException();
 
    两者都会在运行期间抛出异常!
     但是在编译阶段前者的属于抛出一个受检查异常,要求对它进行显式的try..catch 捕获处理或者向上一层方法抛出,否则在编译期间就显示错误!
     后者抛出是运行时异常,在编译阶段不予检查,语法上不会显示任何错误!
     所以简单的通过throw手动抛出受检查异常 和抛出运行时异常,前者要求显式处理,后者不要求作出处理。

3.throw 和throws的区别

    1)throw 是手动抛出异常,throw new **Exception(); 抛出的是某一个异常类型的实例.
    2)throws 是方法抛出异常,写在方法声明处 public void show()throws **Exception,**Exception{} 紧跟throws后的是异常类型,而非异常实例,且可以声明抛出多个异常,同时这些异常类型大多都为 受检查异常类型。
     3)throw 是程序员手动抛出异常,一般可用在某种流程控制,需要显示操作失误情况下可对外抛出异常,进入catch代码块,明示操作有误等。
     throws 方法抛出异常,通常是告知调用此方法者,本方法有可能抛出一个异常,在调用时应当要进行异常监控。且因为throws方法抛出异常为受检查异常类型,这样就从语法上要求更需要对受检查异常类型作出捕获,或者再次向上抛出。

     例如: JDBC中 public static Class forName(String className)throws ClassNotFoundException 加载驱动类时,调用此方法.

    因为方法声明出 throws ClassNotFoundException 就告诉了方法调用者 本方法有可能会发生 ClassNotFoundException 异常,因为需要在调用此方法时格外注意。且此异常类型是受检查类型,那么在编译阶段就更要求用户调用时必须加上 try..catch(){}语句块,或者再次往上方法声明抛出 ClassNotFoundException 交由上层方法声明抛出 。

     throws 方法抛出异常的两种处理方式
Java代码 复制代码 收藏代码
  1. (1)try
  2.         Class.forName("com.microsoft....."); 
  3.     }catch(ClassNotFoundExceptioin e){ 
  4.         ... 
  5.     } 
  6.      
  7.     (2
  8.         public Connection getConnection()throws ClassNotFoundException{ 
  9.             Class.forName("com.microsoft...."); 
  10.         } 
(1)try{
		Class.forName("com.microsoft.....");
	}catch(ClassNotFoundExceptioin e){
		...
	}
	
	(2)
		public Connection getConnection()throws ClassNotFoundException{
			Class.forName("com.microsoft....");
		}


    如果把ClassNotFoundException 改为RuntimeException 就无需处理,那也就失去了方法抛出异常的意义了。

4.常见的运行时异常
  ArithmaticExceptionDemo.java
 
Java代码 复制代码 收藏代码
  1. /**
  2. * 当除数为 0 时的异常  是运行时异常 编译时并不报错 只会在运行时发生
  3. * @author Simon Lv
  4. *
  5. */ 
  6. public class ArithmaticExceptionDemo { 
  7.     //计算两个数相除的结果 
  8.     public int divide(int a,int b){ 
  9.             return a/b; 
  10.         } 
  11.      
  12.     //抛出异常 
  13.     //Exception in thread "main" java.lang.ArithmeticException: / by zero 
  14.     public static void main(String args[]){ 
  15.             ArithmaticExceptionDemo excp = new ArithmaticExceptionDemo(); 
  16.             excp.divide(10,0);   
  17.         } 
/**
 * 当除数为 0 时的异常  是运行时异常 编译时并不报错 只会在运行时发生
 * @author Simon Lv
 *
 */
public class ArithmaticExceptionDemo {
	//计算两个数相除的结果
	public int divide(int a,int b){
			return a/b;
		}
	
	//抛出异常
	//Exception in thread "main" java.lang.ArithmeticException: / by zero
	public static void main(String args[]){
			ArithmaticExceptionDemo excp = new ArithmaticExceptionDemo();
			excp.divide(10,0);	
		}
}


ArrayStoreExceptionDemo.java
Java代码 复制代码 收藏代码
  1. /**
  2. * 演示 试图将一个错误的对象 存储到一个数组时发生的异常  ArrayStoreException
  3. * @author Simon Lv
  4. *
  5. */ 
  6. public class ArrayStoreExceptionDemo { 
  7.      
  8.     //程序的入口 
  9.     public static void main(String arsg[]){ 
  10.             //Object类型 字符串数组 
  11.             Object x[] = new String[3];  
  12.              
  13.             //为了不让程序在编译时就检查报错 就故意写成上述类型 
  14.             //如果写成 下面类型就会直接报错  
  15.             //String x [] = new String[3]; 
  16.              
  17.             try
  18.                 //错误的类型 java.lang.ArrayStoreException: java.lang.Integer 
  19.                  x[0] = new Integer(0);          
  20.             }catch(ArrayStoreException ae){ 
  21.                     System.err.println(ae.toString()+"ok"); 
  22.                     ae.printStackTrace(); 
  23.             } 
  24.              
  25.             System.out.println("程序正常运行!"); 
  26.     } 
  27.  
/**
 * 演示 试图将一个错误的对象 存储到一个数组时发生的异常  ArrayStoreException
 * @author Simon Lv
 * 
 */
public class ArrayStoreExceptionDemo {
	
	//程序的入口
	public static void main(String arsg[]){
			//Object类型 字符串数组
			Object x[] = new String[3]; 
			
			//为了不让程序在编译时就检查报错 就故意写成上述类型
			//如果写成 下面类型就会直接报错 
			//String x [] = new String[3];
			
			try{
				//错误的类型 java.lang.ArrayStoreException: java.lang.Integer
				 x[0] = new Integer(0);         
			}catch(ArrayStoreException ae){
					System.err.println(ae.toString()+"ok");
					ae.printStackTrace();
			}
			
			System.out.println("程序正常运行!");
	}

}


ClassCastExceptionDemo.java
Java代码 复制代码 收藏代码
  1. /**
  2. * 类 类型装换异常 当试图将对象强制转换为不是实例的子类时,抛出该异常 ClassCastException
  3. * @author Simon Lv
  4. *
  5. */ 
  6. public class ClassCastExceptionDemo { 
  7.      
  8.     public static void main(String args[]){ 
  9.              
  10.             Object x = new Integer(12); 
  11.          
  12.             try
  13.                 System.out.println((String)x); 
  14.             }catch(ClassCastException ce){ 
  15.                     System.err.println(ce.toString()); 
  16.                 } 
  17.     } 
  18.  
/**
 * 类 类型装换异常 当试图将对象强制转换为不是实例的子类时,抛出该异常 ClassCastException
 * @author Simon Lv
 *
 */
public class ClassCastExceptionDemo {
	
	public static void main(String args[]){
		    
		    Object x = new Integer(12);
		
			try{
				System.out.println((String)x);
			}catch(ClassCastException ce){
					System.err.println(ce.toString());
				}
	}

}


EmptyStackExceptionDemo.java
Java代码 复制代码 收藏代码
  1. import java.util.EmptyStackException; 
  2. import java.util.Stack; 
  3.  
  4. /**
  5. * 演示堆栈为空的异常 EmptyStackException
  6. * @author Simon Lv
  7. *
  8. */ 
  9. public class EmptyStackExceptionDemo { 
  10.      
  11.     public static void main(String args[]){ 
  12.          
  13.         Stack<String> s = new Stack<String>(); //栈  
  14.         s.push("a");    //先压入 a          压栈 push()方法 
  15.         s.push("b");    //    b 
  16.         s.push("c");    // 最后压入 c 
  17.          
  18.         try
  19.             while(s.size()>0){ 
  20.                 System.out.println( s.pop());    //依次循环将栈中的元素弹出 pop()方法 
  21.             } 
  22.             s.pop(); // 此动作将造成异常,因为栈中所有元素在上面的 循环中就已经弹出,为空栈 
  23.         }catch(EmptyStackException ee){ 
  24.                 System.err.println(ee.toString()); 
  25.                 ee.printStackTrace(); 
  26.             } 
  27.     } 
import java.util.EmptyStackException;
import java.util.Stack;

/**
 * 演示堆栈为空的异常 EmptyStackException
 * @author Simon Lv
 *
 */
public class EmptyStackExceptionDemo {
	
	public static void main(String args[]){
		
		Stack<String> s = new Stack<String>(); //栈 
		s.push("a");    //先压入 a          压栈 push()方法
		s.push("b");    //    b
		s.push("c");    // 最后压入 c
		
		try{
			while(s.size()>0){
				System.out.println(	s.pop());    //依次循环将栈中的元素弹出 pop()方法
			}
			s.pop(); // 此动作将造成异常,因为栈中所有元素在上面的 循环中就已经弹出,为空栈
		}catch(EmptyStackException ee){
				System.err.println(ee.toString());
				ee.printStackTrace();
			}
	}
}


IndexOutOfBoundsExceptionDemo.java
Java代码 复制代码 收藏代码
  1. /**
  2. * 演示 IndexOutOfBoundsException 异常 
  3. * 指某排序索引(例如对数组、字符串或向量的排序)超出范围时抛出。
  4. * @author Simon Lv
  5. *
  6. */ 
  7. public class IndexOutOfBoundsExceptionDemo { 
  8.      
  9.     public static void main(String args[]){      
  10.             int num [] = new int[10]; 
  11.             try
  12.                 //数组10个长度   12次循环就报错 
  13.                 for(int i=0;i<12;i++){ 
  14.                     System.out.println(num[i]); //循环超出范围 
  15.                 } 
  16.             }catch(IndexOutOfBoundsException ie){ 
  17.                     System.err.println(ie.toString()); 
  18.                     ie.printStackTrace(); 
  19.                     System.out.println( ie.getCause()); 
  20.                     System.err.println(ie.getMessage()); 
  21.                      
  22.                 } 
  23.                  
  24.             System.out.println("程序还 能继续执行"); 
  25.         } 
/**
 * 演示 IndexOutOfBoundsException 异常 	
 * 指某排序索引(例如对数组、字符串或向量的排序)超出范围时抛出。
 * @author Simon Lv
 *
 */
public class IndexOutOfBoundsExceptionDemo {
	
	public static void main(String args[]){		
			int num [] = new int[10];
			try{
				//数组10个长度   12次循环就报错
				for(int i=0;i<12;i++){
					System.out.println(num[i]); //循环超出范围
				}
			}catch(IndexOutOfBoundsException ie){
					System.err.println(ie.toString());
					ie.printStackTrace();
					System.out.println(	ie.getCause());
					System.err.println(ie.getMessage());
					
				}
				
			System.out.println("程序还 能继续执行");
		}
}


NegativeArraySizeExceptionDemo.java
Java代码 复制代码 收藏代码
  1. /**
  2. * 演示 NegativeArraySizeException  异常
  3. *  如果应用程序试图创建大小为负的数组,则抛出该异常。
  4. * @author Simon Lv
  5. *
  6. */ 
  7. public class NegativeArraySizeExceptionDemo { 
  8.      
  9.     public static void main(String args[]){ 
  10.              
  11.             try
  12.                 int num [] = new int [-9];   //创建大小为负的数组,则抛出该异常。 
  13.                 System.out.println(num[0]); 
  14.             }catch(NegativeArraySizeException ne){ 
  15.                 System.err.println(ne.toString()); //err红色打印 
  16.                 ne.printStackTrace(); 
  17.             } 
  18.         } 
/**
 * 演示 NegativeArraySizeException  异常
 * 	如果应用程序试图创建大小为负的数组,则抛出该异常。
 * @author Simon Lv
 *
 */
public class NegativeArraySizeExceptionDemo {
	
	public static void main(String args[]){
			
			try{
				int num [] = new int [-9];   //创建大小为负的数组,则抛出该异常。
				System.out.println(num[0]);
			}catch(NegativeArraySizeException ne){
				System.err.println(ne.toString()); //err红色打印
				ne.printStackTrace();
			}
		}
}



NullPointerExceptionDemo.java
Java代码 复制代码 收藏代码
  1. /**
  2. * 当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括:
  3.     调用 null 对象的实例方法。
  4.     访问或修改 null 对象的字段。
  5.     将 null 作为一个数组,获得其长度
  6.     将 null 作为一个数组,访问或修改
  7.     将 null 作为 Throwable 值抛出
  8.     应用程序应该抛出该类的实例,指示其他对 null 对象的非法使用
  9. * @author Simon Lv
  10. *
  11. */ 
  12. public class NullPointerExceptionDemo { 
  13.      
  14.     String nameString;  //类字符串成员变量  默认为null 
  15.      
  16.     public static void main(String args[]){ 
  17.              
  18.             try
  19.                 //返回字符串第一个字符 但将出现异常 相当于 null.charAt(0); 
  20.                 char c = new NullPointerExceptionDemo().nameString.charAt(0); 
  21.                  
  22.                 System.out.println(c); 
  23.             }catch(NullPointerException ne){ 
  24.                 System.err.println(ne.toString()); 
  25.                 ne.printStackTrace(); 
  26.             } 
  27.         } 
  28.  
/**
 * 当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括: 
 	调用 null 对象的实例方法。
 	访问或修改 null 对象的字段。
 	将 null 作为一个数组,获得其长度 
 	将 null 作为一个数组,访问或修改
 	将 null 作为 Throwable 值抛出
 	应用程序应该抛出该类的实例,指示其他对 null 对象的非法使用

 * @author Simon Lv
 *
 */
public class NullPointerExceptionDemo {
	
	String nameString;  //类字符串成员变量  默认为null
	
	public static void main(String args[]){
			
			try{
				//返回字符串第一个字符 但将出现异常 相当于 null.charAt(0);
				char c = new NullPointerExceptionDemo().nameString.charAt(0);
				
				System.out.println(c);
			}catch(NullPointerException ne){
				System.err.println(ne.toString());
				ne.printStackTrace();
			}
		}

}


NumberFormatExceptionDemo.java
Java代码 复制代码 收藏代码
  1. /**
  2. * 演示格式转换异常 NumberFormatException  
  3. * 当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常
  4. * @author Simon Lv
  5. *
  6. */ 
  7. public class NumberFormatExceptionDemo { 
  8.      
  9.     public static void main(String args[]){ 
  10.              
  11.             String a = "3"
  12.             int b = (int)new Integer(a); 
  13.             System.out.println(b);//这个是没有问题的 
  14.              
  15.             try
  16.                 String c = "aa"
  17.                 //java.lang.NumberFormatException: For input string: "aa" 
  18.                 int d = (int)new Integer(c); 
  19.              
  20.                 System.out.println(d);//这个是有问题的 
  21.             }catch(NumberFormatException ne){ 
  22.                 System.err.println(ne.toString()); 
  23.                 ne.printStackTrace(); 
  24.             } 
  25.         } 
/**
 * 演示格式转换异常 NumberFormatException 	
 * 当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常
 * @author Simon Lv
 *
 */
public class NumberFormatExceptionDemo {
	
	public static void main(String args[]){
			
			String a = "3";
			int b = (int)new Integer(a);
			System.out.println(b);//这个是没有问题的
			
			try{
				String c = "aa";
				//java.lang.NumberFormatException: For input string: "aa"
				int d = (int)new Integer(c);
			
				System.out.println(d);//这个是有问题的
			}catch(NumberFormatException ne){
				System.err.println(ne.toString());
				ne.printStackTrace();
			}
		}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值