繁体   English   中英

尝试给出编译错误的块

[英]try block giving compilation error

我正在尝试为我的代码使用try块,但是它会导致编译错误。 代码是:

    public static void main(String[] args) 
    {
        try 
        { 
             //my logic goes here
            return; 
        } 
    }

请帮助解决问题。

如果没有catch / finally块,则无法使用try块。

您的代码应类似于:

    public static void main(String[] args) 
    {
        try 
        { 
            //mu logic goes here
            return; 
        }
        catch (Exception e)
        {

        }    
    }

要么

    public static void main(String[] args) 
    {
        try 
        { 
                //mu logic goes here
            return; 
        }
        finally
        {

        }
    }

或者它可以同时具有多个捕获,但最终只能以任意组合捕获一个。

请参阅Java文档以获取try-catch的基础知识

最基本的正确形式。 这将捕获任何错误并打印出错误消息。

public static void main(String[] args) 
    {
        try 
        { 
         //my logic goes here

        }
        catch(Exception e){
             System.out.println(e);
        }
}

尝试尝试或最终捕获是强制性的,请参考以下代码

public static void main(String[] args) 
{
    try 
    { 
         //my logic goes here
        return; 
    } catch(Exception e){
      // Do Something
    }
}


  public static void main(String[] args) 
{
    try 
    { 
         //my logic goes here
        return; 
    } finally{
      // Do Something
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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