Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve "Unhandled exception type BeansException"

I am a beginner at Spring Batch. I am following this guide to create a HelloWorld of Spring Batch. In the class with main method when I was trying to get Application Context by using new ClassPathXmlApplicationContext("..."), the IDE shows an error message saying

Unhandled exception type BeansException

I cannot solve that error even though I have a catch block that catches all types of exceptions. Refer to the code block below:

    public static void main(String args[]) {
        try {
            //error message appears here
            AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("simpleJob.xml");

            JobParametersBuilder builder = new JobParametersBuilder();
            builder.addString("Date", "12/02/2011");
            jobLauncher.run(job, builder.toJobParameters());

            JobExecution jobExecution = jobRepository.getLastJobExecution(job.getName(), builder.toJobParameters());
            System.out.println(jobExecution.toString());
        } 
        catch(Exception e) {
            e.printStackTrace();
        } 
    }

Then, I tried to solve it by import org.springframework.beans.BeansException; and tried to catch BeansException. Although the unhandled BeansException error was solved but another error message appeared:

No exception of type BeansException can be thrown; an exception type must be a subclass of throwable

Refer to the code block below:

    public static void main(String args[]) {
        try {
            AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("simpleJob.xml");

            JobParametersBuilder builder = new JobParametersBuilder();
            builder.addString("Date", "12/02/2011");
            jobLauncher.run(job, builder.toJobParameters());

            JobExecution jobExecution = jobRepository.getLastJobExecution(job.getName(), builder.toJobParameters());
            System.out.println(jobExecution.toString());
        }
        //error message appears here
        catch(BeansException e) {
            //do something
        } 
        catch(Exception e) {
            e.printStackTrace();
        } 
    }

What is the correct way to solve this error?

Additional note: I do not have my own class named BeansException.

Edit: Stack trace (proceed with error option):

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No exception of type BeansException can be thrown; an exception type must be a subclass of Throwable

    at SpringBatchHelloWorld.BatchLauncher.main(BatchLauncher.java:29)
like image 901
karansky Avatar asked Mar 11 '16 08:03

karansky


People also ask

What is a checked exception in Java?

Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData () throws IOException.

What is an IOException in Java?

Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData () throws IOException. IOException is one of those.

Can a method throw an exception but not a RuntimeException?

That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData () throws IOException.

Should I list or catch an IOException in my throws declaration?

Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.


1 Answers

Thanks to Ken Bekov's comments to the question, I was able to solve this problem and I formulated this solution to officially give this question an answer. Credits should be given to Ken Bekov.

Solution: The problem was due to different version of .jar files included in the build path. The .jar files that should be included are: spring-context-4.2.5.RELEASE.jar, spring-beans-4.2.5.RELEASE.jar and spring-core-4.2.5.RELEASE.jar (notice the same version number - 4.2.5).

As for spring-batch-core-3.0.6.RELEASE.jar, spring-batch-infrastructure-3.0.6.RELEASE.jar and others, they do not necessarily need to have the same version number (4.2.5).

After the correct .jar files were included, there won't even be an "Unhandled exception type BeansException" error message for new ClassPathXmlApplicationContext("...");

like image 62
karansky Avatar answered Sep 21 '22 10:09

karansky