Handle Exception without Catch block Java Example Program
Syntax
static void <method_name>() throws <exception_name>{
try{
//Do something
}finally{
//Do something always even when there is an exception
}
}
Handle Exception without Catch block Example Program
public class HandleExceptionWithoutCatchBlock {
static void doStringOperation() throws Exception{
try{
String text = null;
//Trying to replace character in null string. Exception is caused.
text = text.replaceAll("/", "-");
}finally{
//This code inside finally block will be executed always (Even if there is an exception)
System.out.println("Successfully completed string operation");
}
}
public static void main(String[] args) throws Exception{
doStringOperation();
}
}
Sample Output
Successfully completed string operation
Exception in thread "main" java.lang.NullPointerException
at learnjavaprograms.HandleExceptionWithoutCatchBlock.doStringOperation(HandleExceptionWithoutCatchBlock.java:17)
at learnjavaprograms.HandleExceptionWithoutCatchBlock.main(HandleExceptionWithoutCatchBlock.java:25)
Java Result: 1
Exception Handling Programs
- Exception Handling Example Java Program
- Nested Try Example Java Program
- Throw clause Java Example Program
- Throws clause Java Example Program
- Finally Block Java Example Program
- Try-Catch-Finally Java Example Program
- Pass Argument while throwing Exception Java Example Program
- Multiple Catch Blocks Java Example Program
- Re-Throw Exception Java Example Program
- Print stack trace of the Exception Java Example Program
- Handle Exception without Catch block Java Example Program
- Custom Exception Java Example program
- Termination of Program Due To Unhandled Exception Java Example Program
- Divide by Zero Java Example Program
- Null Pointer Exception Java Example Program
- ArrayIndexOutOfBounds Exception Java Example Program
- Number Format Exception Java Example Program
- String Index Out Of Bounds Exception Java Example Program
- ParseException Java Example Program
Read More Articles
- Multiple Inheritance Using Interface Example Java Program
- Single Inheritance Example Java Program
- Multilevel Inheritance Example Java Program
- Hierarchical Inheritance Example Java Program
- Find all Substrings of a given string Example Java Program
- Create Matrix Example Java Program
- Sum Of Three Numbers Example Java Program
- Heap Sort Example Java Program
- Twin Prime Example Java Program
- Compile Time Polymorphism Example Java Program