动手动脑-异常

Posted kongfanbing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动手动脑-异常相关的知识,希望对你有一定的参考价值。

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");

        }

    }

   

}

输出:

ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException

多层的异常捕获-2

public class CathchWho2{

       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");

        }

    }

}

 ArrayIndexOutOfBoundsException/外层try-catch

import javax.swing.*;

 

class AboutException {

   public static void main(String[] a)

   {

      int i=1, j=0, k;

      k=i/j;

 

 

       try

       {

             

              k = i/j;    // Causes division-by-zero exception

              //throw new Exception("Hello.Exception!");

       }

      

       catch ( ArithmeticException e)

       {

              System.out.println("被0除.  "+ e.getMessage());

       }

      

       catch (Exception e)

       {

              if (e instanceof ArithmeticException)

                     System.out.println("被0除");

              else

              { 

                     System.out.println(e.getMessage());

                    

              }

       }

 

      

       finally

     {

                JOptionPane.showConfirmDialog(null,"OK");

     }

             

  }

}

 

import javax.swing.*;

public class EmbedFinally{

       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");

       

              }

   

       }

}

 

import javax.swing.*;

public class EmbedFinally{

       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

               

                            }

               

               

                            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");

       

              }

   

       }

}

package Tutorial9;

import java.util.*;

class yichang extends Exception{

      

}

class yichang1 extends Exception{

      

}

public class Zuoye {

       static Zuoye A=new Zuoye();

       public static void Dongnao(){

               Scanner in=new Scanner(System.in);

           System.out.print("请输入一个整数:");

           int t=1;

            int m1;

               String m=in.next();

               for(int i=0;i<m.length();i++){

                      if(m.charAt(i)<‘0‘||m.charAt(i)>‘9‘)

                      {

                             try{

                                    throw new yichang();

                             }catch(yichang e){

                                    t=0;

                                    System.out.println("数字格式异常,重新输入");

                                    break;

                             }

                      }

               }

               if(t==1){

                      m1=Integer.parseInt(m);

                      if(m1>100){

                             try{

                                    throw new yichang1();

                             }catch(yichang1 e){

                                    System.out.println("不合法,重新输入");

                             }

                      }

                      else{

                          if(m1<60){

                                 System.out.println("不及格");

                          }

                          else if(m1>=60)

                          {

                                 if(m1>65&&m1<75){

                                        System.out.println("中");

                                 }

                                 else if(m1>=75&&m1<90){

                                        System.out.println("良");

                                 }

                                 else if(m1>=90&&m1<=100){

                                        System.out.println("优");

                                 }

                                 else{

                                        System.out.println("及格");

                                 }

                          }

                      }

               }

               else

                     A.Dongnao();

       }

     public static void main(String args[]){

           while(true){

            A.Dongnao();

           }

     }

}

3.结果截图:技术图片

技术图片

 

 

以上是关于动手动脑-异常的主要内容,如果未能解决你的问题,请参考以下文章

动手动脑

异常处理动手动脑

异常处理(动手动脑)

动手动脑(异常处理)

异常处理---动手动脑及课后作业

动手动脑-异常处理