What is exception propagation?

What is exception propagation ?

I tried on google but not satisfied with the result. Please also explain, if possible, an example. It is recommended to use C ++, php and java.

+5
source share
6 answers

This was surprisingly explained on the Java tutorial exception page .

, , . , a() b(), c(), d(), d() , d c b a, .

+10

: , , " ".

: , , , ( ), . , , , . , . , :

, , 3() method2() method2() method1(). ,

1) 3(), 3() .

2) i.e 2().

3) 2, , 1(), .

enter image description here

:

 class ExceptionPropagation{

  void method3(){
    int result = 100 / 0;  //Exception Generated
  }

  void method2(){
    method3();
  }

  void method1(){
    try{
  method2();
    } catch(Exception e){
  System.out.println("Exception is handled here");
    }
  }

  public static void main(String args[]){
  ExceptionPropagation obj=new ExceptionPropagation();
  obj.method1();
  System.out.println("Continue with Normal Flow...");
  }
}

:

...

.

[1] http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html

[2] http://www.c4learn.com/java/java-exception-propagation/

+10

, , , , , . , JVM, .

java. a >

public class ExceptionTest {
public static void main(String[] args) {
    method1();
    System.out.println("after calling m()");
}

static void method1() {
    method2();
}

static void method2() {
    method3();
}

static void method3() {
    throw new NullPointerException();
}

}

, throw. a >

public class ExceptionTest {
public static void main(String[] args)
                throws FileNotFoundException {
        method1();
        System.out.println("after calling m()");
}

static void method1() throws FileNotFoundException{
        method2();
}

static void method2() throws FileNotFoundException{
        method3();
}

static void method3() throws FileNotFoundException{
        throw new FileNotFoundException();
}

}

(NullPointerException) > Propagating unchecked exception (NullPointerException)

(FileNotFoundException) throw > Propagating checked exception (FileNotFoundException) using throws keyword

: http://www.javamadesoeasy.com/2015/05/exception-propagation-in-java-deep.html

+2

, - , , , , , .., . .

, :

()

()

()

()

c(), , b(), , (), , .

main(), (), b(), c().

+1

, , , . , , ( , ).

class MyClass{

 void myMethod(){
   A a = new A();
   a.doSomething(0);
 }

}

class A{

  double doSomething(int n){
   return 1/n;
  }

}

myMethod , doSomething A, ( myMethod of myClass).

0

, , , , , .

0

All Articles