• Register
0 votes
1.1k 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?

6 5 3
7,540 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.

9 7 4
38,600 points

Related questions

1 vote
1 answer 7 views
7 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 80k points
0 votes
1 answer 11 views
0 votes
1 answer 3 views
3 views
Problem: anyone please help for this problem? exception is caught when exception is not thrown
asked Mar 31 Rohit kr 14.7k points
0 votes
1 answer 4 views
4 views
Problem: Is there someone who can assist me in resolving this issue &ldquo;If an exception has already been thrown, what will the interpreter read next in the program&rdquo;?
asked Apr 1 rakib1 50.8k points
0 votes
1 answer 2.2K views
2.2K views
Problem : I want to create a simple program that will output a string to a text file. Below is my code : import java.io.*; public class JavaTesting { public static void main(String[] args) { File myfile = new File ("myfile.txt"); myfile.getParentFile().mkdirs(); ... for process is 1. As I am very new to Java, I am unable to understand the error. Can anybody help me in resolving the error?
asked Nov 25, 2019 alecxe 7.5k points
0 votes
1 answer 1 view
1 view
Problem: Generally, if any class extends Exception , it becomes a checked exception. Runtime exception also extends Exception. Then how is it unchecked exception? Is it like they have a custom check-in compiler for this special case?
asked 2 days ago ummesalma 22k points
0 votes
1 answer 4 views
4 views
Problem: What are the effects of exceptions on performance in Java?
asked Mar 9 ummesalma 22k points
0 votes
1 answer 5 views
5 views
Problem: Hello! I do not have a lot of knowledge about Java and I keep trying random stuff that I find on the web. I saw following program somewhere: import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] ... .util.HashMap$KeyIterator.next(HashMap.java:1469) at Main.main(Main.java:13) Please explain why this exception is being thrown by this program.
asked Mar 31 Code Learner 9.2k points
0 votes
1 answer 1.1K views
1.1K views
Problem : I want to input some of the details about student to my program. But when I press a save button its giving me the below listed exception error: "Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet."
asked Jan 31, 2020 jwilliam 3.9k points
0 votes
2 answers 520 views
520 views
Problem : I got bellow message from my code Exception in thread "main" java.lang.ArithmeticException: / by zero
asked Nov 1, 2019 peterlaw 6.9k points