Java 已捕获异常,仍给出错误

Java 已捕获异常,仍给出错误,java,exception,Java,Exception,我正在尝试设置GUI的外观。我已经捕获了UnsupportedLookAndFeelException,但是当我编译时,我得到一个错误,它说UnsupportedLookAndFeelException必须被捕获或声明为抛出。错误在这一行:Ne r=new Ne() 代码如下: public static void main(String[] args) { try{ UIManager man = new UIManager(); man.setLookAnd

我正在尝试设置GUI的外观。我已经捕获了UnsupportedLookAndFeelException,但是当我编译时,我得到一个错误,它说UnsupportedLookAndFeelException必须被捕获或声明为抛出。错误在这一行:Ne r=new Ne()

代码如下:

public static void main(String[] args)  {

   try{
      UIManager man = new UIManager();
      man.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel")  ;
   }
   catch(UnsupportedLookAndFeelException ex){}
   catch(Exception ex){}

   SwingUtilities.invokeLater(new Runnable() {
      public void run()  {
         Ne r = new Ne();
         r.setVisible(true);
      }
   });
}

我看不出您的代码如何捕获new Ne()抛出的不受支持的LookandFeelException。为什么不把试捕放在正确的水平上呢?即:

public void run()
{
    try
    {
        Ne r = new Ne();
        r.setVisible(true);

    } catch (UnsupportedLookAndFeelException e)
    {
       // Put some code here to do the right thing.
    }
 }

我看不出您的代码如何捕获new Ne()抛出的不受支持的LookandFeelException。为什么不把试捕放在正确的水平上呢?即:

public void run()
{
    try
    {
        Ne r = new Ne();
        r.setVisible(true);

    } catch (UnsupportedLookAndFeelException e)
    {
       // Put some code here to do the right thing.
    }
 }

我建议阅读更多关于try-catch语句的内容:

总而言之,并不是所有可以引发异常的代码都被try.catch块包围

如果Ne r=new Ne()有错误。。。将其移动到try-catch语句中

public static void main(String[] args)  {

   try{
      UIManager man = new UIManager();
      man.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel")  ;
      SwingUtilities.invokeLater(new Runnable() {
         public void run()  {
            Ne r = new Ne();
            r.setVisible(true);
         }
      });
   }
   catch(UnsupportedLookAndFeelException ex){}
   catch(Exception ex){}
}

如果您使用eclipse之类的IDE,它有一些内置的错误修复方法,这些方法将围绕您所需的代码,这是一个很好的开始,可以确定需要在try-catch块中设置什么

总而言之,并不是所有可以引发异常的代码都被try.catch块包围

如果Ne r=new Ne()有错误。。。将其移动到try-catch语句中

public static void main(String[] args)  {

   try{
      UIManager man = new UIManager();
      man.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel")  ;
      SwingUtilities.invokeLater(new Runnable() {
         public void run()  {
            Ne r = new Ne();
            r.setVisible(true);
         }
      });
   }
   catch(UnsupportedLookAndFeelException ex){}
   catch(Exception ex){}
}

如果您使用eclipse之类的IDE,它有一些内置的错误修复方法,这些方法将围绕您需要的代码,这是一个很好的开始,可以确定在try-catch块中需要设置什么

是您创建的对象类型吗?如果是,构造函数是否有
@throws UnsupportedLookAndFellException
语句?类呢?
Ne
是您制作的对象类型吗?如果是,构造函数是否有
@throws UnsupportedLookAndFellException
语句?上课怎么样?