Various possible combinations of try-catch-finally
Example 1:
class Test1{
public static void main(String[] args){
try
{}
catch(ArithmeticException e)
{}
}
}
Output:
Compile and running successfully.
Example 2:
class Test1{
public static void main(String[] args){
try
{}
catch(ArithmeticException e)
{ }
catch(NullPointerException e)
{}
}
}
Output:
Compile and running successfully.
Example 3:
class Test1{
public static void main(String[] args){
try
{}
catch(ArithmeticException e)
{}
catch(ArithmeticException e)
{}
}
}
Output:
Compile time error.
Test1.java:7: exception java.lang.ArithmeticException has already been caught
catch(ArithmeticException e)
Example 4:
class Test1{
public static void main(String[] args){
try
{}
}
}
Output:
Compile time error
Test1.java:3: 'try' without 'catch' or 'finally'
try
Example 5:
class Test1{
public static void main(String[] args){
catch(Exception e)
{}
}
}
Output:
Compile time error.
Test1.java:3: 'catch' without 'try'
catch(Exception e)
Example 6:
class Test1{
public static void main(String[] args){
try
{}
System.out.println("hello");
catch(Exception e)
{}
}
}
Output:
Compile time error.
Test1.java:3: 'try' without 'catch' or 'finally'
Try
Example 7:
class Test1{
public static void main(String[] args){
try
{}
catch(Exception e)
{}
finally
{}
}
}
Output:
Compile and running successfully.
Example 8:
class Test1{
public static void main(String[] args){
try
{}
finally
{}
}
}
Output:
Compile and running successfully.
Example 9:
class Test1{
public static void main(String[] args){
try
{}
finally
{}
finally
{}
}
}
Output:
Compile time error.
Test1.java:7: 'finally' without 'try'
Finally
Example 10:
class Test1{
public static void main(String[] args){
try
{}
catch(Exception e)
{}
System.out.println("hello");
finally
{}
}
}
Output:
Compile time error.
Test1.java:8: 'finally' without 'try'
Finally
Example 11:
class Test1{
public static void main(String[] args){
try
{}
finally
{}
catch(Exception e)
{}
}
}
Output:
Compile time error.
Test1.java:7: 'catch' without 'try'
catch(Exception e)
Example 12:
class Test1{
public static void main(String[] args){
finally
{}
}
}
Output:
Test1.java:3: 'finally' without 'try'
Finally
Example 13:
class Test1{
public static void main(String[] args){
try
{
try{}
catch(Exception e){}
}
catch(Exception e)
{}
}
}
Output:
Compile and running successfully.
Example 14:
class Test1{
public static void main(String[] args){
try
{ }
catch(Exception e)
{
try{}
finally{}
}
}
}
Output:
Compile and running successfully.
Example 15:
class Test1{
public static void main(String[] args){
try
{ }
catch(Exception e)
{
try{}
catch(Exception e){}
}
finally{
finally{}
}
}
}
Output:
Compile time error.
Test1.java:11: 'finally' without 'try'
finally{}
Example 16:
class Test1{
public static void main(String[] args){
finally{}
try{ }
catch(Exception e){}
}
}
Output:
Compile time error.
Test1.java:3: 'finally' without 'try'
finally{}
Example 17:
class Test1{
public static void main(String[] args){
try{ }
catch(Exception e){}
finally
{
try{}
catch(Exception e){}
finally{}
}
}
}
Output:
Compile and running successfully.
Comments
Post a Comment