3 throws Exception Handling of structural abnormalities java

/ *
* Throws Exception handling 2
*

  • 1, throws + exception type written statement of the method of indicating when this execution method may throw exceptions.
  • When the method of performing exception occurs, an exception will generate class objects in the anomaly, if the object to match the declared types of exceptions throws
  • , They will be thrown, the caller by this method of handling exceptions, the statements following exception code is not executed.
  • 2, the latter throws exception type can have multiple use, spaced apart. Exception type exception type can be produced in the process, it may be its parent.
  • 3, try-catch-finally structure is the real deal out an exception, when the method is invoked not reported abnormal.
  • throws an exception will be thrown only way to the caller method, did not really get rid of the abnormal.
  • 4. The process of rewriting when subclasses override the method can not throw more exceptions than the parent class,
  • The parent class method when there are multiple exceptions thrown exception types that subclass method can not be larger than the parent class method biggest exception types.
  • Subclasses must exception type of the parent class or subclass. Usually the parent class exception type.
  • The reason: When using polymorphism, type of exception try-catch statement in dealing with the type of the parent class, if subclass
  • Exception type is larger than the parent, then the try-catch statement could not be processed, reference to the parent class can not use the object subclasses
  • 5, when the parent class method throws no exception types, methods can not be overridden by subclasses have throws, otherwise in violation of Article.
  • How to choose the way of development to handle exceptions:
  • 1, not the parent class method throws, subclass may override abnormal, processing using the try-catch structure
  • 2. A method of performing call another method B, B in turn calls C, a few methods are performed progressive relationship, then this method can be used several throws
  • Treatment using try-catch unitary structure at the time of execution of the method A, if handled separately, after an abnormality may method C, error data is transmitted to a subsequent process.
  • 3, from the try-catch syntax can be used simultaneously with throws, but seldom, code redundancy.
  • 4, development need to collect error log, change the bug, try-catch only provide temporary plan.
  • Summary: change the bug is inevitable.

*/

package exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Exception_Throws {
	//main方法中处理异常,避免交给JVM处理,防止报错
	public static void main(String[] args) {
		Exception_Throws ex = new Exception_Throws();
		//两种异常统一处理
		 try {
			 ex.method1();
		 }catch(IOException e) {
			 System.out.println("程序出错");
		 }
		 //先在下一层方法中捕获异常进行处理,无法处理再到上一层处理
		 try {
			 ex.method3();
		 }catch(IOException e) {
			 System.out.println("程序出错");
		 }
		 	
	}

	//方法中只抛出异常不处理
	public void method()throws FileNotFoundException,IOException {
		File file = new File("E:\\eclipse-workspace\\Contacts\\src1.txt");
		FileInputStream fis = new FileInputStream(file);
		int date = fis.read();
		while(date != -1) {
			System.out.print((char)date);
			date = fis.read();
		}	
		fis.close();
		System.out.println("----------");
	}
	//方法中只抛出父类异常,交给上一层处理
	public void method1() throws IOException{
		method();		
	}
	
	//在方法中处理异常,只处理一种,另一种抛出给上一层
	public void method3() throws IOException{
		try {
			method();			
		}catch(FileNotFoundException e) {
			System.out.println("找不到文件");
		}
		
	}
	
    public void method4() throws FileNotFoundException,Exception{
		
	}
	
}

class SubException extends Exception_Throws{
//	public void method() throws Exception{//子类重写的方法不能抛出比父类方法中更大的异常
//		
//	}
     public void method() throws FileNotFoundException{
		
	}
     public void method4() throws Exception{
		
	}
    
}
发布了47 篇原创文章 · 获赞 1 · 访问量 1037

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/104500280