Java——异常

Java——异常

异常 : 程序生病了…
Throwable
/
Error Exception
/ /
Uncheck Checked Runtime
Exceiton Exceiton Exceiton

Error: 这类错误不用程序员解决
Exception:
CheckedException: 编译时异常|检查时异常
注意: 如果出现编译时异常,如果不做处理,程序无法执行
通过异常处理方式进行处理
RuntimeExceiton: 发生在程序运行期间
可以通过增强程序的健壮性处理(if),也可以通过异常处理方式进行处理

注意: 如果程序出现异常,不处理程序无法继续执行
常见的运行时异常:
NullPointerException 运行时异常
NegativeArraySizeException 数组长度负值异常
ArrayIndexOutOfBoundsException 数组索引越界异常
ArithmeticException 数学异常
NumberFormatException 数字转换异常

public class ExceptionDemo01 {
		public static void main(String[] args) {
			//编译时异常
			//InputStream is=new FileInputStream("");
		
			//运行时异常
			//NullPointerException
			 String str=null; 
			 //通过增强程序的健壮性处理
			 if(str!=null) {
				 System.out.println(str.length());
			 }
		 
		
			//数组
			//NegativeArraySizeException
			/*
			 * int[] arr=new int[5]; System.out.println(arr[5]);
			 */
			//ArithmeticException
			//System.out.println(6/0);
			//NumberFormatException
			String s="1233a";
			//System.out.println(Integer.valueOf(s)+123); //字符串转为数字
		}
	}

异常处理方式:

  • 1.throws 异常抛出 异常抛出到上一层,谁用谁处理
  • 2.try…catch 异常捕获
  •  try {
     	可能会出现异常的代码
     } catch (FileNotFoundException e) {
     	e.printStackTrace();
     } catch (NullPointerException e) {
     	e.printStackTrace();
     } catch (Exception  e){
     	出现异常可能会执行的代码
     } finally{
     	无论try中的代码是否出现异常,都会执行finally中的代码
     	(一般为资源的关闭..)
     }
    

一旦try中的代码出现异常了,下面代码不会执行,直接执行对应的catch
一个try后面可以跟多个catch,范围大的catch放后面

public class ExceptionDemo02 {
		public static void main(String[] args) {
			try {
				System.out.println("try开始啦");
				test();
			
				System.out.println(5/0);
			
				//System.out.println("".length());
				System.out.println("try结束了");
			} catch (FileNotFoundException e) {
				//	e.printStackTrace();
				System.out.println("文件未找到...");
			} catch (NullPointerException e) {
				e.printStackTrace();
			} catch(Exception e) {
				e.printStackTrace();
			} finally {
				System.out.println("无论程序是否出现异常,都会执行的代码");
			}
		
			System.out.println("main方法继续执行.....");
		}
	
		static void test() throws FileNotFoundException {
			InputStream is=new FileInputStream("D:/heihei.txt");
		}
	}

自定义异常

  • 如何自己创造异常: throw 制造异常对象
  • 如何自定义异常类
  • 先阶段使用的异常类都是java提供的
  • 异常类也是类,直接或者间接的继承自Exception
  • 如果继承自RuntimeException或它的子类,当前的类就是运行时异常
  • 如果直接继承自Exception或者其他的编译时异常,当前的类就是编译时异常
public class DefinedException03 {
	
	public static void main(String[] args) {
		Person p=new Person("zhangsan",13);
		p.setName("zhangsanfeng");
		try {
			p.setAge(-19);
		} catch (AgeException e) {
			e.printStackTrace();
		}
		System.out.println(p);
		
		System.out.println(123);
	}
	
	static void test() throws FileNotFoundException {
		//new DefinedException03();
		//throw new FileNotFoundException(); //普通对象
		
		//InputStream is=new FileInputStream("D:/heihei.txt");
		
	}
}

//自定义异常
	
	class AgeException extends Exception{
		public AgeException() {
		// TODO Auto-generated constructor stub
		}
	
		public AgeException(String message) {
			super(message);
		}
	}

	class Person{
		private String name;
		private int age;
	
		public Person() {
			// TODO Auto-generated constructor stub
		}

		public Person(String name, int age) {
			super();
			this.name = name;
			this.age = age;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) throws AgeException{
			if(age<0 || age>150) {
				//System.out.println("年龄不合法");
				throw new AgeException(age+"不合法");
				//return;
			}
			this.age = age;
		}

		@Override
		public String toString() {
			return "Person [name=" + name + ", age=" + age + "]";
		}	
	}

下一篇,enum

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值