“Java异常处理机制”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
异常的处理
第13行: 第13行:
  
 
==异常的处理==
 
==异常的处理==
 +
===try catch语句===
 
异常怎么来处理呢 ?非常简单,可以将可能发生异常的语句放到try-catch语句当中来处理。
 
异常怎么来处理呢 ?非常简单,可以将可能发生异常的语句放到try-catch语句当中来处理。
  
第26行: 第27行:
  
 
如:
 
如:
  <nowiki>public class MainClass {
+
  <nowiki>
public static void main(String[] args) throws IOException {
+
public class MainClass {
+
+
        public static void divide(int i,int j) {
int i = 10;
+
                  
                 int j = 0;
 
 
                 try{
 
                 try{
 
                     int temp = i/j;                        //此处会产生异常
 
                     int temp = i/j;                        //此处会产生异常
第39行: 第39行:
 
                     //e.printStackTrace();            //打印完整的异常信息
 
                     //e.printStackTrace();            //打印完整的异常信息
 
                 }
 
                 }
 +
        }
 +
public static void main(String[] args)  {
 +
 +
divide(5,0);
 
                 System.out.println("计算结束");
 
                 System.out.println("计算结束");
 
                  
 
                  
第70行: 第74行:
  
 
在运行程序时,上述的try-catch异常处理语句就会捕捉到这个异常,并把它打印出来。
 
在运行程序时,上述的try-catch异常处理语句就会捕捉到这个异常,并把它打印出来。
 +
常用的几种异常:
 +
<nowiki>所有异常的父类:Exception
 +
 +
输入输出异常:IOException
 +
算术异常类:ArithmeticExecption
 +
空指针异常类:NullPointerException
 +
类型强制转换异常:ClassCastException
 +
 +
操作数据库异常:SQLException
 +
 +
文件未找到异常:FileNotFoundException
 +
数组负下标异常:NegativeArrayException
 +
数组下标越界异常:ArrayIndexOutOfBoundsException
 +
违背安全原则异常:SecturityException
 +
文件已结束异常:EOFException
 +
字符串转换为数字异常:NumberFormatException
 +
方法未找到异常:NoSuchMethodException</nowiki>
 +
===throws 语句 ===
 +
try catch是直接处理,处理完成之后程序继续往下执行,throw则是将异常抛给它的上一级处理,程序便不往下执行了。
 +
 +
例子:
 +
<nowiki>public class MainClass {
 +
 +
        public static void divide(int i,int j) throws Exception {
 +
               
 +
              if(j==0){ throw new Exception();}
 +
              int temp = i/j;                        //此处会产生异常
 +
              System.out.println("计算结果:"+temp);
 +
             
 +
        }
 +
public static void main(String[] args)  {
 +
try{
 +
divide(5,0);
 +
        }catch(Exception e){
 +
            System.out.println("出现异常:"+e);    //打印异常信息
 +
            //e.printStackTrace();            //打印完整的异常信息
 +
        }
 +
        System.out.println("计算结束");
 +
               
 +
}
 +
}</nowiki>
 +
  
 
参考文档:
 
参考文档:
  
 
[1] http://www.runoob.com/java/java-exceptions.html
 
[1] http://www.runoob.com/java/java-exceptions.html

2018年5月5日 (六) 02:41的版本

什么是异常

异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的。

比如说,你的代码少了一个分号,那么只说明是语法错误,不算异常;如果你写程序时用0做了除数,如System.out.println(11/0),或者是读取一个并不存在的文件,导致程序偏离了正常的运行轨迹、执行不下去了,这就叫异常。

异常发生的原因有很多,通常包含以下几大类:

   用户输入了非法数据。
   要打开的文件不存在。
   网络通信时连接中断,或者JVM内存溢出。

这些异常有的是因为用户错误引起,有的是程序错误引起的,还有其它一些是因为物理错误引起的。

异常的处理

try catch语句

异常怎么来处理呢 ?非常简单,可以将可能发生异常的语句放到try-catch语句当中来处理。

try
{
   // 位置1
}catch(ExceptionName e1)
{
   //Catch 块
}

把你想写的代码放到位置1当中,如果有异常发生,程序就会及时捕捉到。

如:

public class MainClass {
	
        public static void divide(int i,int j) {
                
                try{
                     int temp = i/j;                        //此处会产生异常
                     System.out.println("计算结果:"+temp);
                }catch(Exception e){
                     System.out.println("出现异常:"+e);    //打印异常信息
                     //e.printStackTrace();            //打印完整的异常信息
                }
        }
	public static void main(String[] args)  {
		
		divide(5,0);		
                System.out.println("计算结束");
                 
	}
}

我们在上述程序编写时,误将0作为了除数,程序就会引发异常。

在运行程序时,上述的try-catch异常处理语句就会捕捉到这个异常,并把它打印出来。

 出现异常:java.lang.ArithmeticException: / by zero

再如:文件的读取

public class MainClass {
	public static void main(String[] args) throws IOException {
                //把内容显示在屏幕上
		try(FileReader fr= new FileReader("reader2.txt")){
					char[] c=new char[20];
					int hasRead=0;
					while((hasRead=fr.read(c))!=-1) {
						System.out.print(new String(c,0,hasRead));
					}
		}catch (Exception e) {
					System.out.println("出现异常:"+e); 
					//e.printStackTrace();			
		}
       }
}

我们在上述程序编写时,误读取了一个并不存在的文件,程序就会引发异常。

在运行程序时,上述的try-catch异常处理语句就会捕捉到这个异常,并把它打印出来。 常用的几种异常:

所有异常的父类:Exception

输入输出异常:IOException
算术异常类:ArithmeticExecption
空指针异常类:NullPointerException
类型强制转换异常:ClassCastException

操作数据库异常:SQLException

文件未找到异常:FileNotFoundException
数组负下标异常:NegativeArrayException
数组下标越界异常:ArrayIndexOutOfBoundsException
违背安全原则异常:SecturityException
文件已结束异常:EOFException
字符串转换为数字异常:NumberFormatException
方法未找到异常:NoSuchMethodException

throws 语句

try catch是直接处理,处理完成之后程序继续往下执行,throw则是将异常抛给它的上一级处理,程序便不往下执行了。

例子:

public class MainClass {
	
        public static void divide(int i,int j) throws Exception {
                
               if(j==0){ throw new Exception();} 
               int temp = i/j;                        //此处会产生异常
               System.out.println("计算结果:"+temp);
               
        }
	public static void main(String[] args)  {
		 try{
			 divide(5,0);	
        }catch(Exception e){
             System.out.println("出现异常:"+e);    //打印异常信息
             //e.printStackTrace();            //打印完整的异常信息
        }		
        System.out.println("计算结束");
                 
	}
}


参考文档:

[1] http://www.runoob.com/java/java-exceptions.html