Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception is never thrown in body of corresponding try statement

Tags:

java

exception

I have a problem with exception handling in Java, here's my code. I got compiler error when I try to run this line: throw new MojException("Bledne dane");. The error is:

exception MojException is never thrown in body of corresponding try statement

Here is the code:

public class Test {
  public static void main(String[] args) throws MojException {
    // TODO Auto-generated method stub

    for(int i=1;i<args.length;i++){
      try{
        Integer.parseInt(args[i-1]);
      }
      catch(MojException e){
        throw new MojException("Bledne dane");
      }
      try{
        WierszTrojkataPascala a = new WierszTrojkataPascala(Integer.parseInt(args[0]));
        System.out.println(args[i]+" : "+a.wspolczynnik(Integer.parseInt(args[i])));
      }
      catch(MojException e){
        throw new MojException(args[i]+" "+e.getMessage());
      }
    }
  }
}

And here is a code of MojException:

public class MojException extends Exception{
    MojException(String s){
        super(s);
    }
}

Can anyone help me with this?

like image 756
user3407967 Avatar asked Mar 24 '14 15:03

user3407967


People also ask

Can I throw exception in try?

The caller has to handle the exception using a try-catch block or propagate the exception. We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program.

What are the 3 types of exceptions?

There are three types of exception—the checked exception, the error and the runtime exception.

What is try in exception handling?

The try statement allows you to define a block of code to be tested for errors while it is being executed. The throw keyword throws an exception when a problem is detected, which lets us create a custom error. The catch statement allows you to define a block of code to be executed if an error occurs in the try block.

How do you handle an exception thrown in Java?

Example. Throw an exception if age is below 18 (print "Access denied"). If age is 18 or older, print "Access granted": public class Main { static void checkAge(int age) throws ArithmeticException { if (age < 18) { throw new ArithmeticException("Access denied - You must be at least 18 years old."); } else { System.


2 Answers

A catch-block in a try statement needs to catch exactly the exception that the code inside the try {}-block can throw (or a super class of that).

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

What you are trying to do is this:

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

This will lead to an compiler error, because your java knows that you are trying to catch an exception that will NEVER EVER EVER occur. Thus you would get: exception ExceptionA is never thrown in body of corresponding try statement.

like image 186
Christian Avatar answered Sep 21 '22 14:09

Christian


As pointed out in the comments, you cannot catch an exception that's not thrown by the code within your try block. Try changing your code to:

try{
    Integer.parseInt(args[i-1]); // this only throws a NumberFormatException
}
catch(NumberFormatException e){
    throw new MojException("Bledne dane");
}

Always check the documentation to see what exceptions are thrown by each method. You may also wish to read up on the subject of checked vs unchecked exceptions before that causes you any confusion in the future.

like image 28
Duncan Jones Avatar answered Sep 19 '22 14:09

Duncan Jones