class a
{
       int ab(int a,int b)
         {    
               if(a<0)throw new b("cuo la");
               return a+b;
               
         }
      public static void main(String [] args)
         {
               a wo=new a();
               System.out.println(wo.ab(2,3));                  
         }
}
class b extends Exception
{
      b(String str)
         {
               super(str);     
         }
}
哪错了 ? 抛出异常不可以这样用吗?

解决方案 »

  1.   

    应该这样public class A

    int ab(int a,int b) throws B 
            {         
               if(a <0)
                        throw   new  B("cuo   la"); 
                  return   a+b; 
                                  

    public   static   void   main(String   []   args) 

                 A wo=new A(); 
                 try {
    System.out.println(wo.ab(2,3));
    } catch (B e) {
    e.printStackTrace();
    }                                     


    class B extends Exception 

    B(String   str) 

         super(str);           


      

  2.   

    异常没有处理(try catch or throws)
      

  3.   

    这样也可以
        
    public   static   void   main(String   []   args) throws Exception
        { 
                    A wo=new A(); 
                    try {
                System.out.println(wo.ab(2,3));
            } catch (B e) {
                e.printStackTrace();
            }                                     
        } 
      

  4.   

    不好意思,写错了public   static   void   main(String   []   args) throws Exception
        { 
                    A wo=new A(); 
                   
                System.out.println(wo.ab(2,3));
                                      
        } 
      

  5.   

    public   static   void   main(String   []   args) 
        { 
                    A wo=new A(); 
                    try {
                System.out.println(wo.ab(2,3));
            } catch (B e) {
                e.printStackTrace();
            }                                     
        } 
    也行 嘎嘎