Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Chained Exceptions

Java Example – Chained Exceptions

Java Examples Java Examples

The following example demonstrates using multiple catch blocks to handle chained exceptions:

Main.java File

public

class

Main

{

public

static

void

main

(

String

args

[

]

)

throws

Exception

{

int

n

=

20

,

result

=

0

;

try

{

result

=

n

/

0

;

System

.

out

.

println

(

"

The result is

"

result

)

;

}

catch

(

ArithmeticException

ex

)

{

System

.

out

.

println

(

"

Arithmetic exception thrown:

"

ex

)

;

try

{

throw

new

NumberFormatException

(

)

;

}

catch

(

NumberFormatException

ex1

)

{

System

.

out

.

println

(

"

Manually throw chained exception:

"

ex1

)

;

}

}

}

}

The output of the above code is:



发算术异常: java.lang.ArithmeticException: / by zero

手动抛出链试异常 : java.lang.NumberFormatException

Java Examples Java Examples