• Register
0 votes

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++){
  catch(MyMojException e){
  throw new MyMojException("The Bledne dane");
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){

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

0 votes
1 answer 7 views
0 votes
1 answer 1.6K 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 814 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 268 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 18 views
I need to find frequency of charts in a string? can anyone here help me?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 582 views
Problem : I am getting bellow error while I try to connect MySql database with Java using connector 8.0.11 java.sql.sqlnontransientconnectionexception: public key retrieval is not allowed
asked Oct 19, 2019 peterlaw 6.9k points
0 votes
2 answers 199 views
Problem : I am new to string handling & getting below error while trying to execute a program java.lang.stringindexoutofboundsexception: string index out of range: 0
asked Oct 23, 2019 peterlaw 6.9k points
1 vote
1 answer 33 views
Problem: Hello Kodlogs, I am a new Java programmer. I am practicing Java I/O file handling. I&rsquo;ve written a Java program to take input a file from my computer. I&rsquo;ve created a file on my desktop and reading it through my these codes: import java ... the program and it's producing an error Exception in thread "main" java.io.FileNotFoundException: file.txt Any idea to overcome this issue?
asked Jun 17, 2020 adamSw 11.3k points
0 votes
1 answer 6 views
Problem: If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. The rethrow expression ( throw without assignment_expression) causes the originally thrown object to be rethrown.
asked Dec 23, 2020 Mashhoodch 5.6k points
0 votes
1 answer 34 views
Problem : Strange windows error is shown repeatedly unhandled exception has occurred in your application. if you click continue the application will
asked Nov 12, 2019 peterlaw 6.9k points