[!INDEX]
- Exception flow ( 7 points ).
- arithmetic ex. Handling Using try-catch ( div/0 )
- code flow + try-catch + rest code + no error ( 1 to 7 code )
- code flow + arithmetic ex. + try-catch + rest code + error ( above code )
- method of ex. object printing( 4 way )
- only try block
- cH block only
- fn/y block only
- 1 tR multiple cH, same eX type
- 1 tR multiple cH, parent-chid eX.
- 1 tR multiple cH, chid-parent eX.
1. Exception flow ( 7 points )
[!important]
- जैसे ही exception मिलेगा concern method एक object बनायेगा और JVM को पास कर देगा |
- this object has three parts -
- exception name ,
- description and
- stack trace
- JVM चेक करेगा की यह exception - handle हो रहा है की नही |
- अगर नही तो JVM , exception object को डिफ़ॉल्ट एक्सेप्शन हैंडलर ( DEH ) को दे देगा |
- लेकिन उससे पहले JVM प्रोग्राम को abnormally terminate कर देगा |
- और ( DEH ) उसको प्रिंट करवा देगा |
- उससे बचने के लीये exception को manually handle करना पड़ेगा |
2. Manual Handling + try-catch + arithmetic exception + error .
public class L02 {
public static void main(String[] args) {
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}
catch(Exception e){ // if dont know the excat exception, use excepion , it will pass it to arithmatic exception
System.out.println(e); // e = exception object name
System.out.println("error in the code, pls check");
}
System.out.println("Program ends Here");
}
}
3. code flow + try-catch + rest code + no error ( 1 to 7 code )
package package2;
public class Main1 {
public static void main(String[] args) {
System.out.println("1");
try{
System.out.println("2");
int a=100, b=2, c;
System.out.println("3");
c=a/b;
System.out.println("4");
System.out.println(c);
System.out.println("5");
}
catch(ArithmeticException e){ // skiped block
System.out.println("6");
System.out.println(e);
System.out.println("7");
}
System.out.println("normal flow");
}
}
1
2
3
4
50
5
normal flow
4. code flow + arithmetic ex. + try-catch + rest code + error ( above code )
package package2;
public class Main1 {
public static void main(String[] args) {
System.out.println("1");
try{
System.out.println("2");
int a=100, b=0, c;
System.out.println("3");
c=a/b;
System.out.println("4");
System.out.println(c);
System.out.println("5");
}catch(ArithmeticException e){
System.out.println("6");
System.out.println(e);
System.out.println("7");
}
System.out.println("normal flow");
}
}
1
2
3
6
java.lang.ArithmeticException: / by zero
7
normal flow
5. method of ex. object printing( 4 way )
package package2;
public class Main1 {
public static void main(String[] args) {
try{
int a=100, b=0, c;
c=a/b;
System.out.println(c);
}catch(ArithmeticException e){
/* whole description */
e.printStackTrace();
/* blow both same - exception name and description */
System.out.println(e);
System.out.println(e.toString());
/* only message */
System.out.println(e.getMessage());
}
}
}
java.lang.ArithmeticException: / by zero
at package2.Main1.main(Main1.java:6)
e = java.lang.ArithmeticException: / by zero
toString = java.lang.ArithmeticException: / by zero
getMessage = / by zero
6. only try block
package package2;
public class Main1 {
public static void main(String[] args) {
try{
// only try block
}
}
}
7. cH block only
package package2;
public class Main1 {
public static void main(String[] args) {
catch(Exception e){
// error: 'catch' without 'try
}
}
}
8. fn/y block only
package package2;
public class Main1 {
public static void main(String[] args) {
finally{
// error: 'finally' without 'try'
}
}
}
9. 1 tR multiple cH, same eX type
package package2;
public class Main1 {
public static void main(String[] args) {
try{
}catch(Exception e){
}catch(Exception e){
// error: exception Exception has already been caught
}
}
}
Unreachable catch block for Exception. It is already handled by the catch block for ExceptionJava(553648315)
10. 1 tR multiple cH, parent-child eX
package package2;
public class Main1 {
public static void main(String[] args) {
try{
}catch(Exception e){
}catch(ArithmeticException e){
// exception ArithmeticException has already been caught
}
}
}
11. 1 tR multiple cH, chid-parent eX
package package2;
public class Main1 {
public static void main(String[] args) {
try{
}catch(ArithmeticException e){
}catch(Exception e){
// works fine
}
}
}