码迷,mamicode.com
首页 > 其他好文 > 详细

动手动脑-异常

时间:2019-11-03 22:16:15      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:visio   tcl   res   ati   on()   scanner   ack   class   exception   

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.结果截图:技术图片

技术图片

 

 

动手动脑-异常

标签:visio   tcl   res   ati   on()   scanner   ack   class   exception   

原文地址:https://www.cnblogs.com/kongfanbing/p/11789219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!