龙空技术网

Java异常之抛出异常详解和代码举例

烦人的星辰 51

前言:

现在同学们对“java中抛出异常”可能比较着重,你们都需要学习一些“java中抛出异常”的相关资讯。那么小编同时在网摘上收集了一些关于“java中抛出异常””的相关资讯,希望咱们能喜欢,你们一起来了解一下吧!

#挑战30天在头条写日记#

抛出异常

1.在捕获异常之前,必须有一段代码生成并抛出一个异常对象。

2.异常类型不同,抛出异常的方法也不同,分为:系统自动抛出的异常、指定方法抛 出的异常。

3.系统定义的异常由系统抛出;指定方法抛出的异常需使用关键字throw或throws;

我们主要讨论由方法抛出的异常用法。

1.抛出异常的方法与调用方法处理异常

异常的产生和处理可以在一个方法内进行;异常的产生和处理也可以不在一个方法中,即一个方法产生异常,由该方法的调用者去处理异常

相对应地,异常有关的方法就有两个:有抛出异常的方法 和处理异常的方法。

2.抛出异常的方法

在方法体内使用throw抛出异常:throw 由异常类产生的对象;在方法头部添加throws抛出异常:

[修饰符] 返回值类型 方法名([参数列表]) throws 异常列表{… …}

由上述两种方式抛出异常,在该方法内就不必提供try-catch-finally 块去处理异常,有调用者的程序处理。

3.处理异常的方法

1.如果程序设计者调用了一个会抛出异常的方法,要么处理这个异常,要么将异常继续抛出。

示例1:方法内部抛出异常、处理异常

package ch09; public class Demo{ 	public static void main(String[] args){ 		int a=5,b=0; 		try{ 			if(b==0) 				throw new ArithmeticException();  //抛出异常 			else 				System.out.println(a+"/"+b+"="+a/b); 		}catch(ArithmeticException e){  //方法内处理异常 			System.out.println("异常:"+e+" 被抛出了!"); 		e.printStackTrace(); 		} 	} } 

示例2:

package ch09; public class Demo{ 	public static double multi(int n){ 		if(n<0) //该方法将抛出非法参数异常 		throw new IllegalArgumentException("求负数阶乘异常");  		double s=1; 		for(int i=1;i<=n;i++)  s=s*i; 		return s; 		} 	public static void main(String[] args){ 		try{ 			int m=Integer.parseInt(args[0]); 			System.out.println(m+"!="+multi(m)); 		}catch(ArrayIndexOutOfBoundsException e){ 			System.out.println("命令行中没提供参数!"); 		}catch(NumberFormatException e){ 			System.out.println("应输入一个【整数】!"); 		}catch(IllegalArgumentException e){  //main方法捕获并处理非法参数异常 			System.out.println("出现的异常是:"+e.toString()); 		}finally{ 			System.out.println("程序运行结束!!"); 		} 	} } 
示例3:
package ch09; public class Demo{ 	static void check(String str1) throws NullPointerException{ 		if(str1.length()>2){ 		str1=null; 		System.out.println(str1.length()); //将抛出空指针异常 	} 	char ch; 	for (int i=0;i<str1.length();i++){ 		ch=str1.charAt(i); 		if(!Character.isDigit(ch)) 		throw new NumberFormatException(); 	} } 	public static void main(String[] args) throws Exception{ 		int num; 		try{ 			check(args[0]); 			num=Integer.parseInt(args[0]); 			if (num>60) 				System.out.println("成绩为:"+num+"  及格"); 			else 				System.out.println("成绩为:"+num+"  不及格"); 		}catch(NullPointerException e){ 			System.out.println("空指针异常:"+e.toString()); 		}catch (NumberFormatException ex){ 			System.out.println("输入的参数不是数值类型"); 		}catch (Exception e){ 			System.out.println("命令行中没有提供参数"); 		} 	} }
2.由方法抛出的异常交给系统处理(需要明确Java对异常强制检查要求的知识)Java 对 Exception 类中的异常可分为受检查型异常和非检查型异常两大类。所有非检查型异常都是RuntimeException类的子类,Java编译器对非检 查型异常不要求进行捕获和处理也能通过编译;而对不是RuntimeException类子类的异常则都是受检查型异常,即如方法 抛出这类异常,调用者必须处理该异常。对于受检查型异常,如果调用者不处理,则必须在最后的 main 方法中将 异常提交给系统。
 示例1:异常最后由main方法提交给系统 package ch09; import java.io.FileInputStream; import java.io.IOException; public class Demo{  	public static void main(String[] args) throws IOException{ //删掉异常将不能通过编译 		FileInputStream fis=new FileInputStream("12.txt"); 	} }  
示例2:
package ch09; import java.io.*; import java.util.Scanner; public class Demo{ 	public static void main(String[] args) throws IOException{ 		String str; 		//以下注释部分用Scanner方式替代 		//BufferedReader buf; 		//buf=new BufferedReader(new InputStreamReader(System.in)); 		Scanner sc=new Scanner(System.in); 		while (true){ 			try{ 				System.out.print("请输入字符串:"); 				//str=buf.readLine(); 				str=sc.nextLine(); 				if(str.length()>0) 					break; 				else 					throw new IOException(); 			}catch (IOException e){ 				System.out.println("必须输入字符串!!"); 				continue; 			} 		} 		String s=str.toUpperCase(); 		System.out.println("转换后的字符串为:"+s); 		sc.close(); 	} } 

标签: #java中抛出异常 #java主动抛出异常怎么写 #java异常如何抛出 #js 异常捕获抛出异常 #java 抛异常怎么写