Checked and unchecked exceptions in java

Original address: http://yangshen998.iteye.com/blog/1311682

[html]  view plain copy  
  1. public class ExceptionTypeTest {  
  2. public void doSomething() throws ArithmeticException{  
  3. System.out.println();  
  4. }  
  5. public static void main(){  
  6. ExceptionTypeTest ett = new ExceptionTypeTest();  
  7. ett.doSomething();  
  8. }  
  9. }  


Question 1: Can the above program compile and pass? and explain why.

Answer: It can be compiled and passed. Analysis: According to common sense, the definition of the doSomething method is to define the ArithmeticException exception, which is called in the main method. Then it should continue to throw or catch. But ArithmeticException is a runtime exception that inherits from RuntimeException. There are two types of exceptions in Java : checked exception (checked exception) and unchecked exception (unchecked exception). For unchecked exception, it is also called RuntimeException (runtime exception) . For runtime exception, the java compiler does not require you to put It catches or must continue to throw, but for checkedexception (checked exception), you must either catch or continue to throw in the method .


Question 2: Can the above program compile and pass by changing ArithmeticException to IOException? and explain why.
Answer: Can't compile. Analysis: IOException extends Exception is a checked exception and must be handled, or must be caught or must be thrown. Summary: Exceptions in java are divided into two categories: checked exception (checked exception) and unchecked exception (unchecked exception). For unchecked exception, it is also called RuntimeException (runtime exception). Several types of unchecked exception (unchecked exception) Processing methods: 1. Capture 2. Continue to throw 3. Do not handle checked exceptions (checked exceptions, except RuntimeException, other exceptions are checked exceptions) Several processing methods: 1. Continue to throw, negative methods, It can always be thrown to the java virtual machine to process 2. Capture with try...catch








注意,对于检查的异常必须处理,或者必须捕获或者必须抛出
1.异常:程序再运行期间发生的不正常事件,它会打断指令的正常流程。
异常都是发生在程序的运行期,编译出现的问题叫语法错误。
1)当程序再运行过程中出现了异常,JVM自动创建一个该类型的异常对象。同时把这个异常对象交给运行时系统。(抛出异常)
2)运行时系统接受到一个异常对象时,它会在产生异常的代码附近查找相应的处理方式。
3)异常的处理方式有两种:
1.捕获并处理:在异常的代码附近显式用try/catch进行处理(不合理),运行时系统捕获后会查询相应的catch处理块,再catch处理块中对该异常进行处理。
2.查看发生异常的方法是否有向上声明异常,有向上声明,向上级查询处理语句,如果没有向上声明,JVM中断程序的运行并处理。用throws向外声明(合理的处理方法)


java.lang.Throwable
|-- Error错误:JVM内部的严重问题。无法恢复。程序人员不用处理。
|--Exception异常:普通的问题。通过合理的处理,程序还可以回到正常执行流程。要求编程人员要进行处理。
|--RuntimeException:也叫非受检异常(unchecked exception).这类异常是编程人员的逻辑问题。应该承担责任。Java编译器不进行强制要求处理。 也就是说,这类异常在程序中,可以进行处理,也可以不处理。
|--非RuntimeException:也叫受检异常(checked exception).这类异常是由一些外部的偶然因素所引起的。Java编译器强制要求处理。也就是说,程序必须进行对这类异常进行处理。
1)非受检的:NullPointerException,ClassCastException,ArrayIndexsOutOfBoundsException,ArithmeticException(算术异常,除0溢出)
2)受检:Exception,FileNotFoundException,IOException,SQLException.
5.异常处理的两种方式:
1).显示用try/catch进行处理(不合理)

[html]  view plain  copy
  1. try{  
  2. //可能会出现异常的代码段;  
  3. }catch(异常类型1 变量名){ //处理指定类型的异常  
  4. //对该类型异常的处理代码段;  
  5. e.printStackTrace();  
  6. }catch(){  
  7. }[finally{  
  8. //无论是否发生异常都要执行的代码段;  
  9. //经常用来清理资源  
  10. }]  


2).向上声明:用throws关键字,向外声明(合理的处理方法)
声明本方法可能会抛出的异常列表。
...方法名(参数列表) throws 异常类型1,异常类型2
向上声明是告诉本方法的调用者,在使用本方法时,应该对这些异常进行处理。


6.手动抛出一个异常:当程序逻辑不符合期望时,要中止后面代码的执行时。
在方法的代码段中,可以使用throw关键字手动抛出一个异常。
注意:如果手动抛出的是受检异常,那么本方法必须进行处理(应该采用向上声明这个异常);如果手动抛出的是非受检异常,那么可以进行处理,也可以不处理。
可以继承Exception来定义一个受检异常。也可以继承自RuntimeException或其子类来定义一个非受检异常。
8.异常概括:一个图两种处理方式.
观察抛出的异常的名字和行号很重要。
应该捕获和处理那些已知如何处理的异常,传递那些不知如何处理的异常。
尽量减少try语句块的体积。

[html]  view plain  copy
  1. for(int i=1;i<=1000;i++){  
  2. try{  
  3. ...  
  4. }catch(...)  
  5. }不合理  
  6. 尽量减少try-catch语句的嵌套  

In the catch block code segment, the stack trace information except the exception should be printed to facilitate debugging.
throws can declare that a method may throw one or more exceptions, separated by ','.
If the declared exception that may be thrown is unchecked, the caller of the method may or may not handle it.
If the declared exception that may be thrown is checked, the caller of this method must handle it.
If the manually thrown exception is a checked exception,
there are two ways to handle it:
1. Display it with try/catch (unreasonable)
2. Use throws to declare it (reasonable handling method)

*****************************************************************************************************************

Exception handling (Exception)

2. Exception handling mechanism:

3. Classification of exceptions:

4. Common exceptions:


7. Custom exception: when some exception information classes related to a specific business are required.

9. Abnormal practice:


Use the throws keyword to declare exceptions that a method may throw

Use the throw keyword to manually throw an exception,

Exceptions that you know how to handle should be caught and handled, and those that you don't know how to handle should be passed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325790243&siteId=291194637