【问题标题】:no suitable constructor found for Exception(int)没有为 Exception(int) 找到合适的构造函数
【发布时间】:2016-11-08 06:34:37
【问题描述】:

我不知道我的代码将流向何处。它有各种错误,我不知道它们是如何工作的。通过类异常的控制流不支持 super(int) 的超级参数,就像它对 super(string) 类型所做的那样。我大多是 Java 新手,这是一个基于用户的异常处理程序。

import java.util.*;
class invalidbal extends Exception{  
    invalidbal (int balance, int transaction){  
    super(balance); 
    super(transaction);
}
class TestCustomException1{  

static void validate(int balance, int transaction)throws invalidbal{  
        if(balance - transaction < 0)  
            throw new InvalidAgeException("You are not eligible to Transact");  
        else  
            System.out.println("Post transaction your current balance is" + (balance - transaction));  
}  

public static void main(String args[]){  
Scanner s = new Scanner(System.in);
        System.out.println("Welcome to transaction checker");
    System.out.println("Please enter the balance:");
    a = s.nextInt();
    System.out.println("Please enter the transaction:");
    b = s.nextInt();
        try{  
            validate(a,b);  
        }catch(Exception m){System.out.println("Exception occured: "+m);}  

    System.out.println("Thank you for checking!");  
        }  
    }  
} 

输出如下。

student@ccf128-OptiPlex-755:~$ javac bal.java
bal.java:4: error: no suitable constructor found for Exception(int)
    super(balance); 
    ^
    constructor Exception.Exception(String) is not applicable
      (argument mismatch; int cannot be converted to String)
    constructor Exception.Exception(Throwable) is not applicable
      (argument mismatch; int cannot be converted to Throwable)
bal.java:5: error: call to super must be first statement in            constructor
    super(transaction);
     ^
bal.java:9: error: Illegal static declaration in inner class      invalidbal.TestCustomException1
    static void validate(int balance, int transaction)throws invalidbal{  
            ^
  modifier 'static' is only allowed in constant variable declarations
bal.java:20: error: cannot find symbol
        a = s.nextInt();
        ^
  symbol:   variable a
  location: class invalidbal.TestCustomException1
bal.java:22: error: cannot find symbol
        b = s.nextInt();
        ^
  symbol:   variable b
  location: class invalidbal.TestCustomException1
bal.java:24: error: cannot find symbol
                validate(a,b);  
                         ^
  symbol:   variable a
  location: class invalidbal.TestCustomException1
bal.java:24: error: cannot find symbol
                validate(a,b);  
                       ^
  symbol:   variable b
  location: class invalidbal.TestCustomException1
bal.java:16: error: Illegal static declaration in inner class   invalidbal.TestCustomException1
    public static void main(String args[]){  
                   ^
  modifier 'static' is only allowed in constant variable declarations
Note: Some messages have been simplified; recompile with -  Xdiags:verbose to get full output
8 errors

【问题讨论】:

标签: java exception exception-handling


【解决方案1】:

在此处输入代码根据 Java 文档 hereException 类没有将输入作为 int 的构造函数,因此您会遇到这些错误:

invalidbal() 构造函数中删除以下代码行

super(balance); 
super(transaction);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多