• Register
0 votes
1.5k views

Problem :

I am facing a problem with the exception handling in Java, following is my code. I am facing compiler error when I try to execute my code. The error is as below:

exception MojException is never thrown in body of corresponding try statement

Below is my code:

public class MyTest { 
public static void main(String[] args) throws MyMojException {
// TODO Auto-generated method stub
for(int m=1;m<args.length;m++){
try{
    Integer.parseInt(args[m-1]);
   }
  catch(MyMojException e){
  throw new MyMojException("The Bledne dane");
  }
 try{
WierszTrojkataPascala w = new WierszTrojkataPascala(Integer.parseInt(args[0]));
System.out.println(args[m]+" : "+w.wspolczynnik(Integer.parseInt(args[m])));
 }
 catch(MojException e){
 throw new MojException(args[m]+" "+e.getMessage());
      }
    }
  }
}

And here is the code for MyMojException:

public class MyMojException extends Exception{
    MyMojException(String str){
        super(str);
    }
}

Can anyone suggest solution on my issue?

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

The catch-block in your try statement needs to catch exactly the exception that the code inside your try {}-block can throw or the super class of that as below

try {
//do something that throws ExceptionA, e.g.
throw new ExceptionA("I am the Exception Alpha!");
}
catch(ExceptionA e) {
//do something to handle the exception, e.g.
System.out.println("Message: " + e.getMessage());
}

But what you are trying to do is as following :

try {
throw new ExceptionB("I am the Exception Bravo!");
}
catch(ExceptionA e) {
System.out.println("Message: " + e.getMessage());
}

This will definitely lead to the compiler error, as java knows that you are trying to catch an exception that will never be thrown. So you will get: exception ExceptionA is never thrown in body of corresponding try statement.

38.6k points

Related questions

0 votes
1 answer 20 views
20 views
Problem: So, what precisely is the problem here : Exception is never thrown in body of corresponding try statement?
asked Jun 20 Mezbah 130k points
0 votes
1 answer 19 views
19 views
Problem: Has anyone else had a similar experience: Exception ioexception is never thrown in body of corresponding try statement?
asked Jun 15 Shima 126k points
0 votes
1 answer 20 views
20 views
Problem: What am I supposed to do here? >This exception is never thrown from the try statement body.
asked May 9 Florina Gulnar 106k points
1 vote
1 answer 39 views
39 views
Problem: Please help me to solve it out : unreported exception filenotfoundexception; must be caught or declared to be thrown
asked Mar 15 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 6 views
6 views
Problem: Exception is caught when exception is not thrown
asked Jul 1 asha 121k points
0 votes
1 answer 15 views
15 views
Problem: Is there a solution for this? > Exception has been thrown by the target of an invocation.
asked May 10 Junia Phoebe 83.8k points
0 votes
1 answer 21 views
21 views
Problem: What is it? >Lỗi exception has been thrown by the target of an invocation.
asked May 8 Rada Gracelynn 104k points
0 votes
1 answer 39 views
39 views
Problem: Can anyone help me with this thing? > Exception has been thrown by the target of an invocation ssis.
asked May 7 Junia Phoebe 83.8k points
0 votes
1 answer 25 views
0 votes
1 answer 106 views
106 views
Problem: anyone please help for this problem? exception is caught when exception is not thrown
asked Mar 31 Rohit kr 17.5k points