简体   繁体   中英

Exception error in java when moving from ubuntu 12.04 to 10.04

I am making a data acquisition software using Sparrow's Kmax . I am writing the code on my ubuntu 12.04 laptop. One of the methods I have created is the following

public double getValueFrom(String widgName) {//Read data from user input text fields
    KmaxWidget widg = tlsh.getKmaxWidget(widgName); 
    String str = widg.getProperty("TEXT");

    double num = 0;
    try {
        num = Double.parseDouble(str);
    }
    catch (Exception e) {
        tlsh.setProperty("STATUSSTR", "Attention! Channel and Energy must be double precision number. i.e. 10, 3.14 etc.");
        tlsh.showWarning("Choose a double precision number.");
        throw e;
    }
    return num; 
} // getValueFrom

When I compile the code on my laptop it goes without any error. When I copy my code to the ubuntu 10.04 desktop I get an exception code

unreported exception java.lang Exception: must be caught or declared to be thrown
throw e;
^

The first thing that crossed my mind was that they have different versions of java. Indeed that was true, so I updated 10.04's java, but again the same error occurs. Any idea on what might be wrong?

EDIT I also added throws Exception after method's arguments but whenever this method is called I get the error(on both systems)

unreported exception Exception; must be caught or declared to be thrown

The problem is coming because of :

 throw e;

Since you are throwing a java.lang.Exception object from your catch block your method signature has to include throws clause stating this and therefore your method signature now becomes (or else your compiler will complain):

  public double getValueFrom(String widgName) throws Exception

This change will resolve the problem.

As the exception says this function it should through the exception to parent class, so you define your function to through exeption like below

public double getValueFrom(String widgName) throws Exception
{
 // body
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM