• Register
0 votes
564 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

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

0 votes
1 answer 937 views
937 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 493 views
493 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 jwilliam 3.9k points
0 votes
2 answers 108 views
108 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
0 votes
1 answer 8 views
8 views
I need to find frequency of charts in a string? can anyone here help me?
asked Sep 22 Daniel Anderson 4k points