Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:44:35+00:00 2026-06-16T13:44:35+00:00

During testing of a class I discovered that my class constructor could take an

  • 0

During testing of a class I discovered that my class constructor could take an “illegal argument”. The constructor was expecting a URI. Obviously if there is nothing suggesting that the passed argument is a URI, then it throws out an IllegalArgumentException.

My constructor originally tested the passed argument to see if it was a valid URI (by creating a file from it).
I then modified this to try the file creation and catch the IllegalArgumentException. In the body of my catch block I simply do:

throw new URISyntaxException(passedArgument, message)

Is this a valid method for catching the possible exceptions that may be thrown, or should I be doing it some other way?

Also, as I found this issue while testing, am I justified in simply modifying the code to throw the exception I expect (and which is a more obvious error to present to the user, and put into the log)?

Edit 1: In response to comments, here is an example of my code:

public myClass (String fileName) throws URISyntaxException {
    try {
        fileToRead = new File(fileName);

        if ( !fileToRead.canRead() ) { //confirm we can read the passed file
            // if not, throw a URI error
            throw new URISyntaxException(fileName, 'bad filename passed, please check path and try again');
        }
    } catch ( IllegalArgumentException e ) {
        throw new URISyntaxException(fileName, 'bad filename passed, please check path and try again');
    }
}

In essence my question is, is it valid to throw a URI exception inside the catch block of the IllegalArgumentException? Is this a valid practice, or could I do this better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:44:36+00:00Added an answer on June 16, 2026 at 1:44 pm

    What you’re doing isn’t really exception chaining, unless you’re actually wrapping the exception you caught inside that one you’re throwing. Instead, you’re simply detecting the violation of a precondition by attempting an operation which you know will throw a certain exception if the precondition is violated, and then reporting the violation by throwing another exception.

    The only potential problem I see with that is whether you really know for sure that the operation you’re attempting will only throw the exception you’re catching in that particular situation. If there might be more than one reason why this operation could throw an IllegalArgumentException, but you’re assuming that the cause will always be a specific one, then the exception you throw to report the issue might end up being misleading (e.g. reporting a valid URI as invalid, just because some other argument was invalid).

    Basically, whenever you’re catching a generic exception and throwing a more specific one instead, there’s a risk that you might be mistaken about the cause of the generic exception. But if you’re sure that no such risk exists — for example, if the code you’re calling clearly documents which exceptions it may throw under what conditions — then it should be fine.


    Edit after actual code was added to the question:

    Mostly unrelated to the general principles discussed above, I see several specific issues with your code.

    1. First, if the File class your code uses is indeed java.io.File, and you’re passing the filename to the constructor as a String rather than as a URI object, then the constructor you’re calling isn’t documented to throw an IllegalArgumentException in any case, so trying to catch one is pointless. The only File constructor that throws those is this one.

    2. Second, the fact that File.canRead() returns false doesn’t necessarily mean that the filename is invalid — it might be perfectly valid, and the file might even exist, but your program might not have the necessary permissions to read it.

    3. Finally, as others have pointed out, I’m not sure what the “URIException” you’re throwing actually is, unless it’s a custom class you’ve written (there is a javax.print.URIException, but that’s an interface, not a class; the only other one I could find is this one from the Apache HTTP Client), but the name, at least, seems completely inappropriate for an exception which you’re using to report an invalid (or unreadable) file name. You really should use some other exception for that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a testing class within my project that is there during the development
During testing, I'm stuck with testing a piece of code that receives a list
We're currently facing some issues during Unit Testing. Our class is multithreading some function
I'm working on a groovy unit-testing class that contains a collection of rules for
I have an immutable class with some private fields that are set during the
During the process of building software applications, you would start testing what you have
Is it possible for the nose unit testing framework to perform tests during the
The EndToEnd test of my application includes loading the releasedlls by hand. During testing
I'm trying to get my head round Unit Testing and there's one more piece
During unit testing I need to decorate my methods with Test attributes as shown

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.