-2

I want to throw a run-time exception from a method which will be caught in another class, which in turn throws a further specific custom exception or does some operation.

    def checkExtension(String fileName,file) {

    String [] arr= Holders.config.bdmserver.defaultfile_ext
       for (int i=0; i < arr.length-1;i++) {
        println("file extension=" + fileName.substring(fileName.length() - 3))
        if (arr[i].equals(fileName.substring(fileName.length() - 4))) {
            println("in if becoz its either .zip or .exe")
            // throw run-time exception 
        }
    }

    def fileSize = (file.getSize())/12000
    if (fileSize > 5 ){
       // throw run-time exception 
    }


}

in different class

catch (run-time exception ex){
                 throw new ApplicationException(BdmsException, messageSource.getMessage("file.upload.failureExtension.message", "Error!! you can not upload a file with this extension", Locale.getDefault()), ex)
        }

How can I do it without creating a new custom exception class?

user207421
  • 305,947
  • 44
  • 307
  • 483
deepti mullur
  • 569
  • 1
  • 4
  • 14
  • What is your aversion to just creating a custom exception in your project? – Tim Biegeleisen Sep 11 '17 at 10:06
  • 2
    This is not Java. Scala perhaps? 'I want to throw a run-time exception from a method which will be caught in another class, which in turn throws a further specific custom exception': so why does it have to be a `RuntimeException`? – user207421 Sep 11 '17 at 10:13
  • first of all, its a groovy code. If you see the logic, its actually uploading a file and I want to throw an exception if someone try's to upload .exe file. – deepti mullur Sep 11 '17 at 10:39
  • 1
    I can always create a custom exception but its just tedious job in this case as I have to follow the organization standards to create custom exception in jar by following other standards for that particular jar. – deepti mullur Sep 11 '17 at 10:41
  • 1
    use IllegalArgumentException with proper message, though using a custom business exception is better – Sikorski Sep 11 '17 at 10:44
  • that's so nice of you Jon. Thank you. I will re-post that question. – deepti mullur Sep 13 '17 at 08:34

3 Answers3

1

You can create your own exception type, with the message that you need, inheriting from RuntimeException. And in your code just put: throw new MyRuntimeException("Put message here");. This way you can be sure you are handling your own exception.

But, if you are not interested in handling this particular exception just use throw new RuntimeException("Put message here");

Remember that Runtime Exceptions are unchecked

jpuriol
  • 362
  • 2
  • 14
0

Why would you not want to create a custom exception yourself?

Anyhow, I'd go for the UnsupportedOperationException in this case.

Marc Van Deuren
  • 121
  • 1
  • 9
0

The only possible way without new type is to create normal exception (Exception or RuntimeException, both support string message), passing some specific message of your choice and then in later code checking against Exception or RuntimeException and matching your message.

try {
    throw new Exception("my specific string message");
catch (Exception e) {
    if ("my specific string message".equals(e.getMessage()) {
        // your code
    else {
        throw e;
    }
}

potential drawback is that now you have to declare exception if it is Exception. Solutions are to use RuntimeException or Unsafe.throwException()

Enerccio
  • 257
  • 1
  • 10
  • 23