Every try block must be followed by the catch or finally blocks and the order of try, catch and finally blocks also important. If try not followed by catch or finally blocks and also order not matched then it gives compile time error.
Here I concluded valid and invalid of try, catch and finally blocks
Valid try, catch and finally blocks
try with a catch block
try{
}
catch(Exception e){
}
try with a multiple catch block
try{
}
catch(ArithmeticException e){
}
catch(NullPointerException e){
}
try with catch and finally block
try{
}
catch(Exception e){
}
finally{
}
try with finally block
try{
}
finally{
}
try with catch and try with the catch block
try{
}
catch(Exception e){
}
try{
}
catch(ArithmeticException e){
}
try with catch and try with finally block
try{
}
catch(Exception e){
}
try{
}
finally{
}
Nested valid try, catch and finally block
try within try block (nested try block)
try{
try{
}
catch(Exception e){
}
}
catch(Exception e){
}
try within try block (nested try block)
try{
try{
}
finally{
}
}
catch(Exception e){
}
try within the catch block
try{
}
catch(Exception e){
try{
}
catch(Exception e1){
}
}
try within finally block
try{
}
finally{
try{
}
catch(Exception e1){
}
}
Invalid try, catch and finally blocks
try without a catch and finally blocks
try{
}
Note: single try block or single catch or finally blocks are incorrect
try with two same exception catch blocks
try{
}
catch(Exception e){
}
catch(Exception e){
}
Order of catch and finally block is incorrect
try{
}
finally{
}
catch(Exception e){
}
SOP statement between try and catch is incorrect
try{
}
System.out.pritnln("");
catch(Exception e){
}
SOP statement between catch and finally is incorrect
try{
}
catch(Exception e){
}
System.out.pritnln("");
finally{
}
Invalid nested try block
Nested try with catch or finally
try{
try{
}
}
catch(Exception e){
}
try{
}
catch(Exception e){
try{
}
}
try{
}
finally{
try{
}
}
No comments:
Post a Comment