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

Java Example – Exception Handling Method

Java Example - Exception Handling Method

Java Examples Java Examples

The following example demonstrates how to use the System class's System.err.println() to display the exception handling method:

ExceptionDemo.java File

class

ExceptionDemo

{

public

static

void

main

(

String

[

]

args

)

{

try

{

throw

new

Exception

(

"

My Exception

"

)

;

}

catch

(

Exception

e

)

{

System

.

err

.

println

(

"

Caught Exception

"

)

;

System

.

err

.

println

(

"

getMessage():

"

e

.

getMessage

(

)

)

;

System

.

err

.

println

(

"

getLocalizedMessage():

"

e

.

getLocalizedMessage

(

)

)

;

System

.

err

.

println

(

"

toString():

"

e

)

;

System

.

err

.

println

(

"

printStackTrace():

"

)

;

e

.

printStackTrace

(

)

;

}

}

}

The output of the above code is:


Caught Exception
getMessage():My Exception
getLocalizedMessage():My Exception
toString():java.lang.Exception: My Exception
printStackTrace():
java.lang.Exception: My Exception
   at ExceptionDemo.main(ExceptionDemo.java:5)

Java Examples Java Examples