import java.io.*;
import java.util.*;
class xmm1 extends Exception{
     private static int m_number=0;
     public xmm1()
     {
     m_number++;
     }
     public String toString()
     {
     return ("new exception appear "+m_number);
     }
}
public class Zuoye 
{    
     public static void main(String[] args)  throws IOException
    {    
     // TO DO, add your application code
     int intx;double doublex;
     String stringx;boolean boolx;    
     try
     {
     Scanner cin=new Scanner(new BufferedInputStream(System.in));
     intx=cin.nextInt();
     doublex=cin.nextDouble();
     boolx=cin.nextBoolean();
     stringx=cin.next();    
     System.out.println(intx);
     System.out.println(doublex);
     System.out.println(boolx);
      System.out.println(stringx);
     }
catch(IOException e)
         {
     System.err.println("Catch text "+e);
         }
     catch(IllegalFormatException e)
    {
      System.err.println("Catch format "+e);
         }
catch(xmm1 e)
        {
     System.err.println("Catch exception define by xmm : "+e);
         }
     catch(ArithmeticException e)
         {
     System.err.println("Catch arithmeticexception "+e);
         }
     finally
     {
    System.out.println("all this do by xmm");
    }
    }
}

解决方案 »

  1.   

    因为你try语句中没有哪句会抛出这两种异常,所以catch中不能捕获
    我修改了你的程序,请看标注的两句
    也就是说,异常要抛出才可以捕获,如果没有哪句抛出过一场,也就根本不能捕获import java.io.*;
    import java.util.*;class xmm1 extends Exception {
    private static int m_number = 0; public xmm1() throws IOException,xmm1{//这里抛出了你后面catch的2个异常
    m_number++;
    } public String toString() {
    return ("new exception appear " + m_number);
    }
    }public class Zuoye {
    public static void main(String[] args) throws IOException {
    // TO DO, add your application code
    int intx;
    double doublex;
    String stringx;
    boolean boolx;
    try {
    xmm1 xm = new xmm1();//这里调用的那个构造方法,这个时候异常才会被抛出,下面才可以捕获
    Scanner cin = new Scanner(new BufferedInputStream(System.in));
    intx = cin.nextInt();
    doublex = cin.nextDouble();
    boolx = cin.nextBoolean();
    stringx = cin.next();
    System.out.println(intx);
    System.out.println(doublex);
    System.out.println(boolx);
    System.out.println(stringx);
    } catch (IOException e) {
    System.err.println("Catch text " + e);
    } catch (IllegalFormatException e) {
    System.err.println("Catch format " + e);
    } catch (xmm1 e) {
    System.err.println("Catch exception define by xmm : " + e);
    } catch (ArithmeticException e) {
    System.err.println("Catch arithmeticexception " + e);
    } finally {
    System.out.println("all this do by xmm");
    }
    }
    }
      

  2.   

    感谢citytalent的解答。
    我认为也是非checked异常需要自己抛出来处理。
    奇怪的就是,抛出位置必须在构造方法后,
    换到main后面就报错(也就是说main后面不需要抛出异常)。
      

  3.   

    异常的处理有2种方式,一种是抛出,留给调用这个方法的人处理
    还有一种是自己try catch首先,不论是throws还是catch了,前提都是有异常的存在
    你的程序中,并不存在IOException,xmm1这两个异常,所以程序不让你catch,因为不存在的东西不能捕捉
    但是我加了xmm1 xm = new xmm1();因为我在这个构造方法中让他抛出2个异常,所以在你下面main方法中调用这个的时候才会产生出异常,这个时候才可以catch
    这个时候你才可以用throws或者catch处理这个一场
    public static void main(String[] args) throws IOException,xmm1 
    // TO DO, add your application code 
    int intx; 
    double doublex; 
    String stringx; 
    boolean boolx; 
    try { 
    xmm1 xm = new xmm1();
    Scanner cin = new Scanner(new BufferedInputStream(System.in)); 
    intx = cin.nextInt(); 
    doublex = cin.nextDouble(); 
    boolx = cin.nextBoolean(); 
    stringx = cin.next(); 
    System.out.println(intx); 
    System.out.println(doublex); 
    System.out.println(boolx); 
    System.out.println(stringx); 
    } catch (IllegalFormatException e) { 
    System.err.println("Catch format " + e); 
    } catch (ArithmeticException e) { 
    System.err.println("Catch arithmeticexception " + e); 
    } finally { 
    System.out.println("all this do by xmm"); 
    } public static void main(String[] args){ 
    // TO DO, add your application code 
    int intx; 
    double doublex; 
    String stringx; 
    boolean boolx; 
    try { 
    xmm1 xm = new xmm1();Scanner cin = new Scanner(new BufferedInputStream(System.in)); 
    intx = cin.nextInt(); 
    doublex = cin.nextDouble(); 
    boolx = cin.nextBoolean(); 
    stringx = cin.next(); 
    System.out.println(intx); 
    System.out.println(doublex); 
    System.out.println(boolx); 
    System.out.println(stringx); 
    catch (IOException e) { 
    System.err.println("Catch text " + e); 
    catch (IllegalFormatException e) { 
    System.err.println("Catch format " + e); 
    catch (xmm1 e) { 
    System.err.println("Catch exception define by xmm : " + e); 
    catch (ArithmeticException e) { 
    System.err.println("Catch arithmeticexception " + e); 
    } finally { 
    System.out.println("all this do by xmm"); 

    你看这里两段红色的部分,这两种方式是处理异常的方式,并不是产生异常的方式
    产生一场的方式是:public xmm1() throws IOException,xmm1{//这里抛出了你后面catch的2个异常 
    m_number++; 

    xmm1 xm = new xmm1();这里执行的时候才会产生异常
      

  4.   

    谢谢citytalent 精心的解答。
    最近正在努力学习中。