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

Java Example – Multithreading Exception Handling

Java Example - Multithreading Exception Handling

Java Examples Java Examples

The following example demonstrates multithreaded exception handling:

Main.java File

class

MyThread

extends

Thread

{

public

void

run

(

)

{

System

.

out

.

println

(

"

Throwing in

"

"

MyThread

"

)

;

throw

new

RuntimeException

(

)

;

}

}

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

MyThread

t

=

new

MyThread

(

)

;

t

.

start

(

)

;

try

{

Thread

.

sleep

(

1000

)

;

}

catch

(

Exception

x

)

{

System

.

out

.

println

(

"

Caught it

"

x

)

;

}

System

.

out

.

println

(

"

Exiting main

"

)

;

}

}

The output of the above code is:


Throwing in MyThread
Exception in thread "Thread-0" java.lang.RuntimeException
        at testapp.MyThread.run(Main.java:19)
Exiting main

Java Examples Java Examples