JAVA OOP第六章 异常

package cn.happy1.com;
import java.util.*;

import org.apache.log4j.Logger;

public class Log4 {

	static Logger logger2=Logger.getLogger(Log4.class);
		
		
		public static void main(String[] args) {
			

			Scanner input=new Scanner(System.in); 
		    System.out.println("请输入第一个数:");
		    int num1=input.nextInt();
		    System.out.println("请输入第二个数:");
		    
		    if(input.hasNextInt()){
		    	int num2=input.nextInt();
		    	if(num2==0){
		    		System.err.println("除数不能为0");
		    		
		    		//记录到硬盘上的一个文件中
	//一个日志级别:5个(由低到高)debug info warn error fatal	    		
		    		
		    		logger2.info("除数不能为0");
		    		
		    	    System.exit(1);
		    	}else{
		    		int result=num1/num2;
		    		System.out.println(result);
		    	}
		    }
		}

}


========

package cn.happy1.com;

public class Person {

	private String name="";
	private int age=0;
	private String sex="男";
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}

	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public void setSet(String sex) throws Exception{
		if("男".equals(sex)||"女".equals(sex)){
			this.sex=sex;
		}else{
			
			throw new Exception("性别必须是\"男\"或者\"女\"!");
		}
	}
	public void setAge(int age) throws Exception{
		if(age>=0&&age<=100){
			this.age=age;
		}else{
			
			throw new Exception("年龄必须是1-100之间!");
		}
	}
		public void print(){
			System.out.println(this.name+"("+this.sex+","+this.age+"岁)");
			
		}
	
}

package cn.happy1.com;
import java.util.*;
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("请输入(1-3)之间的整数:");
try {
	int num=input.nextInt();
	switch(num){
		case 1: 
			System.out.println("C#编程");
			break;
		case 2:
			System.out.println("Java编程");
			break;
		case 3:
			System.out.println("SQL基础");
			break;
			default:
				System.err.println("数字不是1-3");
	break;	
	}
} 
 catch(InputMismatchException ime){
	 
	 System.err.println("输入的不是整数");
	 System.exit(1);
 }catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}finally{
	System.err.println("欢迎提出建议!!!");
	
}
	}
}

=========

package cn.happy1.com;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test1 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.println("请输入被除数:");
		try{
    int num1=input.nextInt();
    System.out.println("请输入除数:");
    int num2=input.nextInt();
    System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
   return;//finally会被执行
	}
		catch(InputMismatchException e){
			System.err.println("俩数都得是整数");
			
		}
		catch(ArithmeticException e){
			System.err.println("除数不能是0");
			
		}
		catch(Exception e){
			
			System.err.println("其他错误");
			e.printStackTrace();
			System.exit(1);;//finally不会会被执行
		}
		finally{
			 System.out.println("感谢使用本程序!!");
			
		}
		}

}

package cn.happy1.com;

import java.util.Scanner;

public class Test2 {

	public static void divide() throws Exception{
		
		Scanner input=new Scanner(System.in);
		
		System.out.println("请输入被除数:");
    int num1=input.nextInt();
    System.out.println("请输入除数:");
    int num2=input.nextInt();
    System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
	}
	public static void main(String[] args) {
		
   
		try{
		divide();	
			
		}
		catch(Exception e){
			
			System.out.println("除数不能为0");
			e.printStackTrace();
		}
	}

}

package cn.happy1.com;

public class Test3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
  Person person=new Person();
  try{
	  person.setSet("女");
	  person.setAge(102);
	  person.print();
  }catch(Exception e){
	  
	  e.printStackTrace();
	  
  }
	}

}

package cn.happy2.com;

import java.util.Scanner;

public class MySS {

	public void SS() throws Exception{
		
		try {
			Scanner input=new Scanner(System.in);
			
			System.out.println("请输入被除数:");
	    int num1=input.nextInt();
	    System.out.println("请输入除数:");
	    int num2=input.nextInt();
	    System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
		
		} catch (Exception e) {
		//throw new 
		}
	}
}

package cn.happy2.com;

public class Test1 {

	public static void main(String[] args) throws Exception{
		
		
		// TODO Auto-generated method stub

		
		try {
		int i=5/0;
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("=--=--");
		}
		System.out.println("try 之外的");
	}

}

package cn.happy5.com;

public class Test {

	public static void foo(int i){

try {
	if(i==1){
		throw new Exception();
	}
	
	System.out.println("1");
} catch (Exception e) {
System.out.println("2");	
}finally{
	System.out.println("3");

}System.out.println("4");}
public static void main(String[] args){
	
	foo(1);
}
}
	



package cn.happy6.com;

import org.apache.log4j.Logger;

public class Index {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Logger lo=Logger.getLogger("");
try{int[] num=new int[2];
for(int i=0;i

你可能感兴趣的:(JAVA OOP第六章 异常)