⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceptionhandle.java

📁 出错处理 出错处理
💻 JAVA
字号:
import java.io.*;
public class ExceptionHandle {

	/**
	
       12.1 编程实现具有捕捉4种不同异常的程序(除数为零,文件不存在,文件打
       不开,数组下标越界)。

	 */
	public static void main(String[] args) 
	{
		System.out.println("四个异常程序:");
		//除数为零
		try{
			int a=10;
		    int b=0;
			int result=a/b;
			//System.out.println("This will not be printed.");
		}catch(ArithmeticException e)
		{ 
			System.out.println("错误(除数不能为零):"+e);
		}
		//文件不存在
		InputStreamReader cin=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(cin);
		File f= new File("e:\\ExceptionHandle.java");
		System.out.println("该文件是否存在:"+f.exists());
		try {
			FileInputStream fin=new FileInputStream(f);
		} catch (FileNotFoundException e) {
			System.out.println("错误(文件不存在):"+e);
			//e.printStackTrace();
		}
		//文件打不开
		try{
			FileOutputStream fos=new FileOutputStream(new File("D:\\11.txt"));
			fos.write(12345);
			System.out.println("文件已打开");
		}
		catch(Exception n)
		{ 
		    System.out.println("错误(文件打不开)"+n);
		}
		/*
		try{
			
		}catch(Exception e){System.out.println("错误(文件无法打开):"+e);}
		*/
		//数组下标越界
		try{
			int array[]=new int[5];
			for(int i=0;i<=array.length;i++)
				array[i]=i+1;
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("错误(数组下标越界)"+e);
		}
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -