课程5 1905

(1)

public class CatchWho 

    public static void main(String[] args) 
    { 
        try 
 { 
             try 
  { 
                 throw new ArrayIndexOutOfBoundsException(); 
             } 
             catch(ArrayIndexOutOfBoundsException e) 
  { 
                 System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch"); 
             }
 
            throw new ArithmeticException(); 
        } 
        catch(ArithmeticException e) 
 { 
            System.out.println("发生ArithmeticException"); 
        } 
        catch(ArrayIndexOutOfBoundsException e) 
 { 
           System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
        } 
    } 
}

(2)

public class CatchWho2 

    public static void main(String[] args) 
    { 
        try 
 {
             try 
  { 
                 throw new ArrayIndexOutOfBoundsException(); 
             } 
             catch(ArithmeticException e) 
  { 
                 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
             }
            throw new ArithmeticException(); 
        } 
        catch(ArithmeticException e) {
  
            System.out.println("发生ArithmeticException"); 
        } 
        catch(ArrayIndexOutOfBoundsException e) 
 { 
            System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
        } 
    } 
}

(3)


public class EmbededFinally 
{

    
 public static void main(String args[]) 
 {
        
  int result;
        
  try 
  {
            
   System.out.println("in Level 1");

           
    try 
   {
                
    System.out.println("in Level 2");
  // result=100/0;  //Level 2
               
     try 
    {
                   
      System.out.println("in Level 3");
                      
      result=100/0;  //Level 3
                
    } 
                
    catch (Exception e) 
    {
                    
     System.out.println("Level 3:" + e.getClass().toString());
                
    }
                
                
    finally 
    {
                    
     System.out.println("In Level 3 finally");
                
    }
                
               
    // result=100/0;  //Level 2

            
    }
            
   catch (Exception e) 
   {
               
     System.out.println("Level 2:" + e.getClass().toString());
           
    }
    finally 
   {
                
    System.out.println("In Level 2 finally");
           
    }
             
   // result = 100 / 0;  //level 1
        
  } 
        
  catch (Exception e) 
  {
            
   System.out.println("Level 1:" + e.getClass().toString());
        
  }
        
  finally 
  {
           
.    System.out.println("In Level 1 finally");
        
  }
    
 }

}

(4)


public class SystemExitAndFinally 
{

    
 public static void main(String[] args)
    
 {
        
  try
  {

            
   System.out.println("in main");
            
   throw new Exception("Exception is thrown in main");

              //System.exit(0);

        
  }
        
  catch(Exception e)

         {
            
   System.out.println(e.getMessage());
            
   System.exit(0);
        
  }
        
  finally
        
  {
            
   System.out.println("in finally");
        
  }
    
 }


}

(5)

// UsingExceptions.java
// Demonstrating the getMessage and printStackTrace
// methods inherited into all exception classes.
public class PrintExceptionStack 
{
   public static void main( String args[] )
   {
      try 
      {
         method1();
      }
      catch ( Exception e ) 
      {
         System.err.println( e.getMessage() + "\n" );
         e.printStackTrace();
      }
   }

   public static void method1() throws Exception
   {
      method2();
   }

   public static void method2() throws Exception
   {
      method3();
   }

   public static void method3() throws Exception
   {
      throw new Exception( "Exception thrown in method3" );
   }
}

(6)Java多层嵌套异常处理的基本流程

 异常是程序中的一些错误,但并不是所有的错误都是异常,错误有时候是可以避免的。异常的对象有两个来源,一是Java运行时环境自动抛出系统生成的异常,而不管你是否愿意捕获和处理,它总要被抛出!比如除数为0的异常。二是程序员自己抛出的异常,这个异常可以是程序员自己定义的,也可以是Java语言中定义的,用throw 关键字抛出异常,这种异常常用来向调用者汇报异常的一些信息。

       异常是针对方法来说的,抛出、声明抛出、捕获和处理异常都是在方法中进行的。 

       Java异常处理通过5个关键字try、catch、throw、throws、finally进行管理。基本过程是用try语句块包住要监视的语句,如果在try语句块内出现异常,则异常会被抛出,你的代码在catch语句块中可以捕获到这个异常并做处理;还有以部分系统生成的异常在Java运行时自动抛出。你也可以通过throws关键字在方法上声明该方法要抛出异常,然后在方法内部通过throw抛出异常对象。finally语句块会在方法执行return之前执行。

       使用try...catch捕获异常。

        执行try块里的业务逻辑代码时出现异常,系统自动生成一个异常对象,该异常对象被提交给Java运行时环境,这个过程被称为抛出(throw)异常。Java运行时环境收到异常对象时,会寻找能处理该异常对象的catch块,如果找到合适的catch块并把该异常对象交给catch块处理那这个过程被称为捕获(catch)异常;如果Java运行时环境找不到捕获异常的catch块,则运行时环境终止,Java程序也将退出。

(7)

import java.io.*;
public class ThrowMultiExceptionsDemo 

    public static void main(String[] args) 
    { 
      try 
      { 
            throwsTest(); 
       } 
        catch(IOException e) 
 { 
            System.out.println("捕捉异常"); 
        }
    }

    private static void throwsTest()  throws ArithmeticException,IOException 
    { 
        System.out.println("这只是一个测试"); 
        // 程序处理过程假设发生异常
        throw new IOException(); 
        //throw new ArithmeticException(); 
    } 
}

(8)


import java.io.*;


public class OverrideThrows
{
 public void test()throws IOException
 {
  FileInputStream fis = new FileInputStream("a.txt");
 }
}
class Sub extends OverrideThrows
{
 //如果test方法声明抛出了比父类方法更大的异常,比如Exception
    //则代码将无法编译……
 public void test() throws FileNotFoundException
 {
            //...
 }
}

原文地址:https://www.cnblogs.com/leiyu1905/p/14170082.html