[英]try block giving compilation error
I am trying to use try block for my code, but it gives compilation error. 我正在尝试为我的代码使用try块,但是它会导致编译错误。 Code is :
代码是:
public static void main(String[] args)
{
try
{
//my logic goes here
return;
}
}
Please help solving problem. 请帮助解决问题。
try block can not be used without catch/finally block. 如果没有catch / finally块,则无法使用try块。
your code should be like : 您的代码应类似于:
public static void main(String[] args)
{
try
{
//mu logic goes here
return;
}
catch (Exception e)
{
}
}
or 要么
public static void main(String[] args)
{
try
{
//mu logic goes here
return;
}
finally
{
}
}
or it can have both multiple catch but only one finally in any combinations. 或者它可以同时具有多个捕获,但最终只能以任意组合捕获一个。
refer to java docs for basic learning of try-catch 请参阅Java文档以获取try-catch的基础知识
Most basic correct form. 最基本的正确形式。 This will catch any error and print out the error message.
这将捕获任何错误并打印出错误消息。
public static void main(String[] args)
{
try
{
//my logic goes here
}
catch(Exception e){
System.out.println(e);
}
}
catch with try or finally is mandatory, please refer to below code 尝试尝试或最终捕获是强制性的,请参考以下代码
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.