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

Java Example – Custom Exception

Java Example - Custom Exception

Java Examples Java Examples

The following example demonstrates how to implement a custom exception by extending Exception:

TestInput.java File

class

WrongInputException

extends

Exception

{

//

Custom class

WrongInputException

(

String

s

)

{

super

(

s

)

;

}

}

class

Input

{

void

method

(

)

throws

WrongInputException

{

throw

new

WrongInputException

(

"

Wrong input

"

)

;

//

Throwing the custom class

}

}

class

TestInput

{

public

static

void

main

(

String

[

]

args

)

{

try

{

new

Input

(

)

.

method

(

)

;

}

catch

(

WrongInputException

wie

)

{

System

.

out

.

println

(

wie

.

getMessage

(

)

)

;

}

}

}

The output of the above code is:



Wrong input

Java Examples Java Examples