Java尝试捕获循环在1循环后没有捕获

时间:2014-03-01 02:58:48

标签: java try-catch vlcj

在一个循环之后,程序在新组件行处抛出异常并退出。如何循环直到用户选择有效的文件?

    while(!next){
            NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), folderChooser());
            try{
                EmbeddedMediaPlayerComponent a = new EmbeddedMediaPlayerComponent();
                a.release();
                next = true;
            }
            catch(Exception e){
                next = false;
            }
    }

错误:

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class uk.co.caprica.vlcj.binding.LibVlc
    at uk.co.caprica.vlcj.binding.LibVlcFactory.create(LibVlcFactory.java:158)
    at uk.co.caprica.vlcj.player.MediaPlayerFactory.<init>(MediaPlayerFactory.java:236)
    at uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent.onGetMediaPlayerFactory(EmbeddedMediaPlayerComponent.java:278)
    at uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent.<init>(EmbeddedMediaPlayerComponent.java:168)
    at sv1.MainRun.try1(MainRun.java:107)
    at sv1.MainRun.<init>(MainRun.java:82)
    at sv1.Start.main(Start.java:7)

3 个答案:

答案 0 :(得分:1)

NoClassDefFoundError ncdx2 = null;
try  {

   ...

}  catch(NoClassDefFoundError ncdx)  {
   ncdx2 = ncdx;
}
if(ncdx != null)  {
   System.out.println("Try again: " + ncdx.getMessage());
}  else  {
   next = true;
}

这样可行,但绝对不建议使用没有错误作为循环的终止条件。是否有任何方式来检查这个 崩溃?希望您使用的API有一些关于某些isOkeyDokey()函数的信息。如果没有,你可能会做一些反思。任何事都比使用错误替代逻辑更好。

更多信息:https://www.google.com/search?q=how+to+catch+an+error+in+java

答案 1 :(得分:0)

见以下修改

try
{
while(!next)
{
   NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), folderChooser());

   EmbeddedMediaPlayerComponent a = new EmbeddedMediaPlayerComponent();
   a.release();
   next = true;
}
}
catch(Exception e){
next = false;
}

答案 2 :(得分:-1)

好吧,我会尝试移动试用线

while(!next){
try{
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), folderChooser());

            EmbeddedMediaPlayerComponent a = new EmbeddedMediaPlayerComponent();
            a.release();
        }
        catch(Exception e){

        }
}