Java Example - Custom Exception
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