We can create the custom checked exception by extending the Exception class as follow. Here We are adding some courses to list and we are throwing an exception if the given course is not there in list.
package com.sample.java.testing;
public class CustomCheckedException extends Exception{
String course;
public CustomCheckedException(String message, String course){
super(message);
this.course = course;
}
public CustomCheckedException(String message, Throwable throwable){
super(message, throwable);
}
@Override
public String getMessage() {
return course + super.getMessage();
}
}
package com.sample.java.testing;
import java.util.ArrayList;
public class CustomCheckedExceptionDemo {
static ArrayList<String> arrayList = new ArrayList<>();
public void addCourse(){
arrayList.add("java");
arrayList.add("j2ee");
}
public boolean checkCourseName(String course) throws CustomCheckedException {
if (arrayList.contains(course)) {
return true;
} else {
throw new CustomCheckedException(
" course was not found in our course list", course);
}
}
public static void main(String[] args) {
CustomCheckedExceptionDemo checkedExceptionDemo = new CustomCheckedExceptionDemo();
checkedExceptionDemo.addCourse();
try {
checkedExceptionDemo.checkCourseName("spring");
} catch (CustomCheckedException e) {
e.printStackTrace();
}
}
}
Output:
com.sample.java.testing.CustomCheckedException: spring course was not found in our course list
at com.sample.java.testing.CustomCheckedExceptionDemo.checkCourseName(CustomCheckedExceptionDemo.java:18)
at com.sample.java.testing.CustomCheckedExceptionDemo.main(CustomCheckedExceptionDemo.java:28)
package com.sample.java.testing;
public class CustomCheckedException extends Exception{
String course;
public CustomCheckedException(String message, String course){
super(message);
this.course = course;
}
public CustomCheckedException(String message, Throwable throwable){
super(message, throwable);
}
@Override
public String getMessage() {
return course + super.getMessage();
}
}
package com.sample.java.testing;
import java.util.ArrayList;
public class CustomCheckedExceptionDemo {
static ArrayList<String> arrayList = new ArrayList<>();
public void addCourse(){
arrayList.add("java");
arrayList.add("j2ee");
}
public boolean checkCourseName(String course) throws CustomCheckedException {
if (arrayList.contains(course)) {
return true;
} else {
throw new CustomCheckedException(
" course was not found in our course list", course);
}
}
public static void main(String[] args) {
CustomCheckedExceptionDemo checkedExceptionDemo = new CustomCheckedExceptionDemo();
checkedExceptionDemo.addCourse();
try {
checkedExceptionDemo.checkCourseName("spring");
} catch (CustomCheckedException e) {
e.printStackTrace();
}
}
}
Output:
com.sample.java.testing.CustomCheckedException: spring course was not found in our course list
at com.sample.java.testing.CustomCheckedExceptionDemo.checkCourseName(CustomCheckedExceptionDemo.java:18)
at com.sample.java.testing.CustomCheckedExceptionDemo.main(CustomCheckedExceptionDemo.java:28)
No comments:
Post a Comment