Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Example to catch a NoSuchMethodException Posted by lose_the_ego on 22 Jul 2004 at 6:20 AM
Hi,

I am new to Java Swing programming. Following is the partial code:
// Some Class and code
private void initComponents() {

//other components and other code go here...

lbl_status = new javax.swing.JLabel();
try{
lbl_status.setBounds(60, 150, 330, 21);
}
catch(NoSuchMethodException e){
e.printStackTrace();
System.out.println("Method = " +e);
}
// other code...
}

I do have a main function in this class. I am running J2SDK v.1.4.2_05
When I compile, I get the following error message:
"exception.java.lang.NoSuchMethodException is never thrown in body of corresponding try statement"
What am I doing wrong? I have a long list of components and method calls. One of them is taking this method call error when I port this over to UNIX where I am running a lower version of Java(1.3). I want to catch this 'NoSuchMethodException' to find out which method call is the culprit but I am not able to do so. I am developing this in a windows XP environment.

Thanks for your answer and time.
Report
Re: Example to catch a NoSuchMethodException Posted by DNP Solutions on 22 Jul 2004 at 9:07 AM
Hi back.

What you're doing wrong is exactly what the compiler's telling you. You're trying to catch an error that's not even potentially thrown by the code.

You see, the try statment catches the exceptions in its block only. Think of it this way:
public void myMethod() {

    // Exceptions here will not be caught.

    try {

        // Only exceptions here will be caught.

    } catch (NoSuchMethodException e) {

        // Exceptions here won't get caught, obviously.

    }

    // Exceptions here won't get caught either.

}


Since the only statement in your try block is a call to a component's setBounds method, and the setBounds method has no possibility of throwing a NoSuchMethodException, you're trying to catch an error that will never be thrown.

If you are getting NoSuchMethodExceptions from some other place, then you need your try block to be where they are coming from.

Hope I'm understanding your problem correctly...

DNP
Report
Re: Example to catch a NoSuchMethodException Posted by lose_the_ego on 22 Jul 2004 at 10:14 AM
Thanks for your quick feedback. Here is some more info'. I compiled and ran this program in Windows. Runs perfectly fine. When I moved the class file to UNIX and run it there, I get a 'NoSuchMethodError'. I don't know whether the JRE version matters here. It seems like it is not recognizing one of the swing component method calls. It doesn't tell me which method call broke it. How can I capture this problem method call? Based on your note, how can I know which method call can throw a NoSuchMethod exception. Is there a way I can do a catch all type at a global level for this 'specific' exception? Thanks again.

: Hi back.
:
: What you're doing wrong is exactly what the compiler's telling you. You're trying to catch an error that's not even potentially thrown by the code.
:
: You see, the try statment catches the exceptions in its block only. Think of it this way:
:
public void myMethod() {
: 
:     // Exceptions here will not be caught.
: 
:     try {
: 
:         // Only exceptions here will be caught.
: 
:     } catch (NoSuchMethodException e) {
: 
:         // Exceptions here won't get caught, obviously.
: 
:     }
: 
:     // Exceptions here won't get caught either.
: 
: }

:
: Since the only statement in your try block is a call to a component's setBounds method, and the setBounds method has no possibility of throwing a NoSuchMethodException, you're trying to catch an error that will never be thrown.
:
: If you are getting NoSuchMethodExceptions from some other place, then you need your try block to be where they are coming from.
:
: Hope I'm understanding your problem correctly...
:
: DNP
:

Report
Re: Example to catch a NoSuchMethodException Posted by DNP Solutions on 22 Jul 2004 at 6:58 PM
Why don't you post me the stack trace? I might be able to give you something more concrete.

I haven't encountered this problem, but I do know that there is one place where a potential for NoSuchMethodException is present in Swing. It's in the ProxyLazyValue inner class (and its friends and family). That class uses reflection to call methods based on string versions of their name. Normally, it's pretty tightly controlled, checked so that sort of thing doesn't happen, but if someone starts subclassing the various elements of the look and feel and isn't vigilant about them, it can sneak in.

Anyway, give me the stack trace, and I'll take a look at it.

DNP

: Thanks for your quick feedback. Here is some more info'. I compiled and ran this program in Windows. Runs perfectly fine. When I moved the class file to UNIX and run it there, I get a 'NoSuchMethodError'. I don't know whether the JRE version matters here. It seems like it is not recognizing one of the swing component method calls. It doesn't tell me which method call broke it. How can I capture this problem method call? Based on your note, how can I know which method call can throw a NoSuchMethod exception. Is there a way I can do a catch all type at a global level for this 'specific' exception? Thanks again.
:
: : Hi back.
: :
: : What you're doing wrong is exactly what the compiler's telling you. You're trying to catch an error that's not even potentially thrown by the code.
: :
: : You see, the try statment catches the exceptions in its block only. Think of it this way:
: :
public void myMethod() {
: : 
: :     // Exceptions here will not be caught.
: : 
: :     try {
: : 
: :         // Only exceptions here will be caught.
: : 
: :     } catch (NoSuchMethodException e) {
: : 
: :         // Exceptions here won't get caught, obviously.
: : 
: :     }
: : 
: :     // Exceptions here won't get caught either.
: : 
: : }

: :
: : Since the only statement in your try block is a call to a component's setBounds method, and the setBounds method has no possibility of throwing a NoSuchMethodException, you're trying to catch an error that will never be thrown.
: :
: : If you are getting NoSuchMethodExceptions from some other place, then you need your try block to be where they are coming from.
: :
: : Hope I'm understanding your problem correctly...
: :
: : DNP
: :
:
:

Report
Re: Example to catch a NoSuchMethodException Posted by lose_the_ego on 23 Jul 2004 at 1:27 PM
DNP, I have another problem now. The first one might still exist. Sorry to bug you. After I compile this program in Windows, why do I have multiple class files for the same program (like name of the program say is xxx.class, I have xxx$1.class, xxx$2.class, xxx$3.class etc.) in my DOS directory. I can't move these files to UNIX because the $ means something there. I moved the one xxx.class to UNIX and when I run it there, I get the following (stack trace):

Exception in thread "main" java.lang.NoClassDefFoundError: DldRepUnix$1
at DldRepUnix.initComponents(DldRepUnix.java:86)
at DldRepUnix.<init>(DldRepUnix.java:18)
at DldRepUnix.main(DldRepUnix.java:181)

*** Note: xxx = DldRepUnix (name of the program)

Is this normal to have these type of class files for one program which has only one class xxx? I am new to Java Programming too. So excuse my ignorance here.

Thanks.

: Why don't you post me the stack trace? I might be able to give you something more concrete.
:
: I haven't encountered this problem, but I do know that there is one place where a potential for NoSuchMethodException is present in Swing. It's in the ProxyLazyValue inner class (and its friends and family). That class uses reflection to call methods based on string versions of their name. Normally, it's pretty tightly controlled, checked so that sort of thing doesn't happen, but if someone starts subclassing the various elements of the look and feel and isn't vigilant about them, it can sneak in.
:
: Anyway, give me the stack trace, and I'll take a look at it.
:
: DNP
:
: : Thanks for your quick feedback. Here is some more info'. I compiled and ran this program in Windows. Runs perfectly fine. When I moved the class file to UNIX and run it there, I get a 'NoSuchMethodError'. I don't know whether the JRE version matters here. It seems like it is not recognizing one of the swing component method calls. It doesn't tell me which method call broke it. How can I capture this problem method call? Based on your note, how can I know which method call can throw a NoSuchMethod exception. Is there a way I can do a catch all type at a global level for this 'specific' exception? Thanks again.
: :
: : : Hi back.
: : :
: : : What you're doing wrong is exactly what the compiler's telling you. You're trying to catch an error that's not even potentially thrown by the code.
: : :
: : : You see, the try statment catches the exceptions in its block only. Think of it this way:
: : :
public void myMethod() {
: : : 
: : :     // Exceptions here will not be caught.
: : : 
: : :     try {
: : : 
: : :         // Only exceptions here will be caught.
: : : 
: : :     } catch (NoSuchMethodException e) {
: : : 
: : :         // Exceptions here won't get caught, obviously.
: : : 
: : :     }
: : : 
: : :     // Exceptions here won't get caught either.
: : : 
: : : }

: : :
: : : Since the only statement in your try block is a call to a component's setBounds method, and the setBounds method has no possibility of throwing a NoSuchMethodException, you're trying to catch an error that will never be thrown.
: : :
: : : If you are getting NoSuchMethodExceptions from some other place, then you need your try block to be where they are coming from.
: : :
: : : Hope I'm understanding your problem correctly...
: : :
: : : DNP
: : :
: :
: :
:
:

Report
Yes, it's very common. Posted by DNP Solutions on 29 Jul 2004 at 7:16 AM
Any time your class has an inner class, static class or anonymous class contained within it, you'll have these. They contain the bytecode for those other classes.

When a number follows the "$," that's because it's an anonymous class. At some point, you're creating "in-line subclasses," as it were.

DNP
Report
Re: Example to catch a NoSuchMethodException Posted by arb123 on 23 Jul 2004 at 1:56 AM
: When I compile, I get the following error message:
: "exception.java.lang.NoSuchMethodException is never thrown in body of corresponding try statement"
: What am I doing wrong?

Surely this is just a warning, not an error that prevents complilation?

You should probably avoid calls that won't be present in older target VMs, especially if they're going to be called alot. However, you could use a strategy like this:
import java.awt.Component;

public class BackwardsCompatible {

    private static boolean SET_BOUNDS = false;
    private static boolean TEST_SET_BOUNDS = true;
    public static void setBounds(Component c, int i1, int i2, int i3, int i4) {
        if (TEST_SET_BOUNDS) {
            Class[] params =
                { Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, };
            try {
                Component.class.getMethod("setBounds", params);
                SET_BOUNDS = true;
            } catch (NoSuchMethodException e) {
                //failsafe
            }
            TEST_SET_BOUNDS = false;
        }
        if(SET_BOUNDS) {
			c.setBounds(i1, i2, i3, i4);
        } else {
        	//do something else?
        }
    }

}



---------------------------------
HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html

Report
Re: Example to catch a NoSuchMethodException Posted by lose_the_ego on 23 Jul 2004 at 1:30 PM
I will try this out. I have another problem now. I will let you know. Thanks for the reply!

: : When I compile, I get the following error message:
: : "exception.java.lang.NoSuchMethodException is never thrown in body of corresponding try statement"
: : What am I doing wrong?
:
: Surely this is just a warning, not an error that prevents complilation?
:
: You should probably avoid calls that won't be present in older target VMs, especially if they're going to be called alot. However, you could use a strategy like this:
:
: import java.awt.Component;
: 
: public class BackwardsCompatible {
: 
:     private static boolean SET_BOUNDS = false;
:     private static boolean TEST_SET_BOUNDS = true;
:     public static void setBounds(Component c, int i1, int i2, int i3, int i4) {
:         if (TEST_SET_BOUNDS) {
:             Class[] params =
:                 { Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, };
:             try {
:                 Component.class.getMethod("setBounds", params);
:                 SET_BOUNDS = true;
:             } catch (NoSuchMethodException e) {
:                 //failsafe
:             }
:             TEST_SET_BOUNDS = false;
:         }
:         if(SET_BOUNDS) {
: 			c.setBounds(i1, i2, i3, i4);
:         } else {
:         	//do something else?
:         }
:     }
: 
: }
: 
: 

:
: ---------------------------------
: HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html
:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.