For explicitly throwing the exception, the keyword throw is used. The keyword throw is normally used within a method. We can not throw multiple exceptions using throw.
Using throw
• For
explicitly throwing the exception, the keyword throw is used. The keyword throw
is normally used within a method. We can not throw multiple exceptions using
throw.
Java Programming Example
class
Exception Throw
{
static
void fun(int a,int b)
{
int c;
if(b==0)
throw
new ArithmeticException("Divide By Zero!!!");
else
c=a/b;
}
public
static void main(String args[])
{
int a=5;
fun(a,0);
}
}
Output
Exception
in thread "main" java.lang.ArithmeticException: Divide By Zero!!!
at
ExceptionThrow.fun(ExceptionThrow.java:7)
at
ExceptionThrow.main(ExceptionThrow.java:14)
Difference
between throw and throws
Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : with Example Exception Handling Java Programs - Using throw
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation