3 ms·
The idea is that if an exception has to be thrown, it will throw as early as possible, making it pretty obvious during development that there's a bug - whenever
by AYBABTME 13y ago
The idea is that if an exception has to be thrown, it will throw as early as possible, making it pretty obvious during development that there's a bug - whenever you try to run your app it will exit with an error about the MalformedUrl. Notice I said he/she should catch the exception in the static block and log it, pretty hard to have an issue tracking where the error comes from.
You can't check at compile time that the Url is valid, but if as soon as you load the program, it crashes with this exception, it will be hard to ignore and the mistake won't be hidden. Since you catch the exception that's thrown a static time, there's no ClassDefNotFoundException.
See examples of this concept in Go:
http://golang.org/pkg/regexp/#MustCompile http://golang.org/pkg/regexp/#MustCompile
http://golang.org/pkg/text/template/#Must http://golang.org/pkg/text/template/#Must
The better solution would be to have a compile time error, but I don't think Java can do that.