Exception Concept in java
Exception
+--------+
| Object |
+--------+
|
|
+-----------+
| Throwable |
+-----------+
/ \
/ \
+-------+ +-----------+
| Error | | Exception |
+-------+ +-----------+
/ | \ / | \
\________/ \______/ \
+------------------+
unchecked checked | RuntimeException |
+------------------+
/ | | \
\_________________/
unchecked
Exception hadling using try, catch, finally
try {
// statements that might cause exceptions
// possibly including function calls
} catch ( exception-1 id-1 ) {
// statements to handle this exception
} catch ( exception-2 id-2 ) {
// statements to handle this exception
.
.
.
} finally {
// statements to execute every time this try block executes
}
concept:
Each catch clause specifies the type of one exception, and provides a name for it (similar to the way a function header specifies the type and name of a parameter). Java exceptions are objects, so the statements in a catch clause can refer to the thrown exception object using the specified name.
The finally clause is optional.
In general, there can be one or more catch clauses. If there is a finally clause, there can be zero catch clauses.
concept 1;
what will output :
package com.exception;
import java.util.ArrayList;
public class exceptionTest {
public String getName() {
try{
return "kumud";
}
catch(Exception ex)
{
System.out.println("Finally Exception## try in getName method");
}
finally
{
System.out.println("Finally Exception## finally in getName method");
return "kumud5";
}
}
public static void main(String [] args)
{
ArrayList ar= new ArrayList();
exceptionTest exp= new exceptionTest();
try
{
System.out.println("Exception is"+exp.getName());
}
catch(Exception ex)
{
System.out.println("Exception##"+ex);
}
finally
{
System.out.println("Finally Exception##");
}
}
}
Output:
Finally Exception## finally in getName method
Exception iskumud5
Finally Exception##
Exp:
in try block there is return method as well as in finally block also . but output is return from finally block reason behind that finally will execute either
exception is found or not in any any condition while return value is given in try or catch block.
concept 2:
In which case finally block will not executed
package com.exception;
import java.util.ArrayList;
public class exceptionTest {
public String getName() {
try{
System.exit(0);
return "kumud";
}
catch(Exception ex)
{
System.out.println("Finally Exception## try in getName method");
}
finally
{
System.out.println("Finally Exception## finally in getName method");
return "kumud5";
}
}
public static void main(String [] args)
{
ArrayList ar= new ArrayList();
exceptionTest exp= new exceptionTest();
try
{
System.out.println("Main Try block");
System.out.println("Exception is"+exp.getName());
}
catch(Exception ex)
{
System.out.println("Exception##"+ex);
}
finally
{
System.out.println("Finally Exception##");
}
}
}
Output:
Main Try block
Expl: if you written System.exit(0) in try block in only that condition finally block will not execute
Exception
+--------+
| Object |
+--------+
|
|
+-----------+
| Throwable |
+-----------+
/ \
/ \
+-------+ +-----------+
| Error | | Exception |
+-------+ +-----------+
/ | \ / | \
\________/ \______/ \
+------------------+
unchecked checked | RuntimeException |
+------------------+
/ | | \
\_________________/
unchecked
Exception hadling using try, catch, finally
try {
// statements that might cause exceptions
// possibly including function calls
} catch ( exception-1 id-1 ) {
// statements to handle this exception
} catch ( exception-2 id-2 ) {
// statements to handle this exception
.
.
.
} finally {
// statements to execute every time this try block executes
}
concept:
Each catch clause specifies the type of one exception, and provides a name for it (similar to the way a function header specifies the type and name of a parameter). Java exceptions are objects, so the statements in a catch clause can refer to the thrown exception object using the specified name.
The finally clause is optional.
In general, there can be one or more catch clauses. If there is a finally clause, there can be zero catch clauses.
concept 1;
what will output :
package com.exception;
import java.util.ArrayList;
public class exceptionTest {
public String getName() {
try{
return "kumud";
}
catch(Exception ex)
{
System.out.println("Finally Exception## try in getName method");
}
finally
{
System.out.println("Finally Exception## finally in getName method");
return "kumud5";
}
}
public static void main(String [] args)
{
ArrayList ar= new ArrayList();
exceptionTest exp= new exceptionTest();
try
{
System.out.println("Exception is"+exp.getName());
}
catch(Exception ex)
{
System.out.println("Exception##"+ex);
}
finally
{
System.out.println("Finally Exception##");
}
}
}
Output:
Finally Exception## finally in getName method
Exception iskumud5
Finally Exception##
Exp:
in try block there is return method as well as in finally block also . but output is return from finally block reason behind that finally will execute either
exception is found or not in any any condition while return value is given in try or catch block.
concept 2:
In which case finally block will not executed
package com.exception;
import java.util.ArrayList;
public class exceptionTest {
public String getName() {
try{
System.exit(0);
return "kumud";
}
catch(Exception ex)
{
System.out.println("Finally Exception## try in getName method");
}
finally
{
System.out.println("Finally Exception## finally in getName method");
return "kumud5";
}
}
public static void main(String [] args)
{
ArrayList ar= new ArrayList();
exceptionTest exp= new exceptionTest();
try
{
System.out.println("Main Try block");
System.out.println("Exception is"+exp.getName());
}
catch(Exception ex)
{
System.out.println("Exception##"+ex);
}
finally
{
System.out.println("Finally Exception##");
}
}
}
Output:
Main Try block
Expl: if you written System.exit(0) in try block in only that condition finally block will not execute
No comments:
Post a Comment