java study notes: java abnormal

First, abnormal
1, JVM default is how to handle the exception of
the main function upon receipt of this unusual problem, there are two approaches:

  • a: own deal with the issue, and then continue to run
  • b: the way he did for processing, it is to call the main jvm to deal with, jvm have a default exception handling mechanism, it is the abnormal processing. And the name of the exception, the exception information. Abnormal position of the print on the console, and the program will stop running

try ... catch to handle exceptions ways 1

A:异常处理的两种方式
	a:try…catch…finally
	b:throws
B:try...catch处理异常的基本格式    	
  try	{
  	  可能出现问题的代码 ;
  }catch(异常名 变量名){
	  针对问题的处理 ;
  }finally{
	  释放资源;
  }  
变形格式:
	try	{
		可能出现问题的代码 ;
	}catch(异常名 变量名){
		针对问题的处理 ;
	}
注意事项:
  1: try中的代码越少越好 	
  2: catch中要做处理,哪怕是一条输出语句也可以.(不能将异常信息隐藏)

try ... catch to handle exceptions way 2

   try {
   可能出现问题的代码 ;
    }catch(异常名1 变量名1){
    对异常的处理方式 ;
    }catch (异常名2 变量名2){
    对异常的处理方式 ;
 	}....   
 JDK1.7中对多个catch的变形格式  
  try {
   可能出现问题的代码 ;
  }catch(异常名1 | 异常名2 | ....   变量名){
   对异常的处理方案 ;
  }
好处: 就是简化了代码
弊端: 对多个异常的处理方式是一致 
注意事项:
  1:能明确的尽量明确,不要用大的来处理。
  2:平级关系的异常谁前谁后无所谓,如果出现了子父关系,父必须在后面。
  3:多个异常之间只能是平级的关系,不能出现子父类的继承关系

2. Case Presentation: Abnormal compile and runtime exception difference

// 定义一个日期字符串
	String dateStr = "2015-11/17";	
	// 创建一个SimpleDateFormat对象
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;		
	// 调用方法
	Date date = null;
	try {
		date = dateFormat.parse(dateStr);
	} catch (ParseException e) {
		System.out.println("解析异常......");
	}		
	// 输出
	System.out.println(date);

3, several common methods of Throwable

     a:getMessage():		获取异常信息,返回字符串。
	b:toString():			获取异常类名和异常信息,返回字符串。
	c:printStackTrace():	获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。

4, throws the exception manner

A:throws的方式处理异常
	定义功能方法时,需要把出现的问题暴露出来让调用者去处理。
	那么就通过throws在方法上标识。

5, the difference between throw and throws as well as an overview of

A:throw的概述:在功能方法内部出现某种情况,程序不能继续运行,需要进行跳转时,就用throw
B:throws和throw的区别
	a:throws
		用在方法声明后面,跟的是异常类名
		可以跟多个异常类名,用逗号隔开
		表示抛出异常,由该方法的调用者来处理
		throws表示出现异常的一种可能性,并不一定会发生这些异常
	b:throw
		用在方法体内,跟的是异常对象名
		只能抛出一个异常对象名
		这个异常对象可以是编译期异常对象,可以是运行期异常对象
		表示抛出异常,由方法体内的语句处理
		throw则是抛出了异常,执行throw则一定抛出了某种异常
		private static void show1() {	
	// 定义两个int类型的变量
	int a = 23 ;
	int b = 0 ;		
	// 判断
	if(b == 0){	
		// 使用throw抛出一个异常对象
		throw new ArithmeticException("除数为0.") ;	
	}else {
		// 输出
		System.out.println(a / b);
	}

6, characteristics and finally the role of keywords

A:finally的特点
	被finally控制的语句体一定会执行(前提 jvm没有停止)
	特殊情况:在执行到finally之前jvm退出了(比如System.exit(0))
B:finally的作用:	用于释放资源,在IO流操作和数据库操作中会见到
// 日期字符串
	String dateStr = "2015-11/17";	
	// 创建SimpleDateFormat对象
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
	// 把日期字符串解析成一个日期对象
	Date date = null;
	try {
		date = dateFormat.parse(dateStr);
	} catch (ParseException e) {
		e.printStackTrace();
		System.exit(0) ;
	} finally {
		System.out.println("哦,被执行了.................");
	}
	// 输出
	System.out.println(date);

7, finally keyword face questions

A:面试题1:	final,finally和finalize的区别          
	 * 	final: 是一个状态修饰符, 可以用来修饰类 , 变量 , 成员方法. 
			被修饰的类不能被子类继承, 修饰的变量其实是一个常量不能被再次赋值
	 * 		修饰的方法不能被子类重写
	 * 	finally:用在try...catch...语句中 , 作用: 释放资源 . 特点: 始终被执行(JVM不能退出)
	 * 	finalize: Obejct类中的一个方法,用来回收垃圾
B:面试题2:	如果catch里面有return语句,请问finally的代码还会执行吗?如果会,请问是在return前还是return后。
		答:会执行, 在return前

8, overview and basic custom exception use

A:为什么需要自定义异常:
 因为在以后的开发过程中,我们可能会遇到各种问题,
而Jdk不可能针对每一种问题都给出具体的异常类与之对应.
    为了满足需求,我们就需要自定义异常.
举例:考试成绩必须在0-100之间,不满足就产生异常。
B:自定义异常概述 需要将我们自定义的异常类纳入到我们的异常体系中
	继承自Exception
	继承自RuntimeException

9, unusual precautions and how to use exception handling

A:异常注意事项(针对编译期异常)
	a:子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子类,或者子类不抛出异常也是可以的。(父亲坏了,儿子不能比父亲更坏)
	b:如果父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常,或者子类不抛出异常也是可以的。
	c:如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常发生,那么子类只能try,不能throws
B:如何使用异常处理
	原则:如果该功能内部可以将问题处理,用try,如果处理不了,交由调用者处理,这是用throws
	区别:
		后续程序需要继续运行就try
		后续程序不需要继续运行就throws
		如果JDK没有提供对应的异常,需要自定义异常。
Published 24 original articles · won praise 11 · views 2052

Guess you like

Origin blog.csdn.net/weixin_43791069/article/details/86516731