[英]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
{
}
}
或者它可以同時具有多個捕獲,但最終只能以任意組合捕獲一個。
最基本的正確形式。 這將捕獲任何錯誤並打印出錯誤消息。
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.