Description:
Without using try catch block. If you do not want to
explicitly make try catch block then to you program write throws Exception to
your method where Exception handling is required
Code:
class
exceptionHandle {
public
static
void
main(String args[]) throws Exception {
int
a = 5;
int
b = a / 0;
System.out.println(b);
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException:
/ by zero
at exceptionHandle.main(exceptionHandle.java:7)
Now the other way to use Exception Handling is by using try
catch block shown in following code sample:
class
exceptionHandle {
public
static
void
main(String[] args) {
try
{
int
a = 5;
int
b = a / 0;
System.out.println(b);
}
catch (Exception e) {
System.out.println(e);
}
}
}
Output:
java.lang.ArithmeticException: / by zero
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.