Know the error code in downloading a file

I use the following code to download a url from the web:


class GeneralDownloader {
    static final int MAX_SIZE=50000;
    static final int BUF_SIZE=512;

    public GeneralDownloader(){
    public void run(String pSourceURL, String pDestinationFile){

        InputStream in;
        try {
            URL idURL=new URL(pSourceURL);
            try {

                System.out.println( "file="+idURL.getFile().substring(1));
                if (pDestinationFile==null) {
                //Connect with remoteHost
                System.out.println( "Trying to connect with remote host.." );
                DataInputStream remoteIn = new DataInputStream(idURL.openStream());
                //Reading data from remote host
                System.out.println( "Reading data from remote host.." );

                byte urlData[]=new byte[MAX_SIZE];
                //read whole resource
                int count_all=0;
                int count_1=BUF_SIZE;
                byte buf[]=new byte[BUF_SIZE];

                while(count_1>0) {
                    if (count_1>0) {
                        System.arraycopy(buf, 0, urlData, count_all, count_1);

                //Write  data to file
                FileOutputStream out=new FileOutputStream(pDestinationFile);
                System.out.println( "Write  data to file " +pDestinationFile+". Data len="+count_all+" bytes.");
                out.write(urlData, 0, count_all);

            }catch (IOException io){io.printStackTrace();}

        } catch (MalformedURLException m) {

    public static void main(String a[]) {
        GeneralDownloader mGeneralDownloader = new GeneralDownloader();"", "index.html");


If there is an error (like the file doesn't exist or i have no authorization for this) i want to get a notification that will contain the error number and message like in Netscape.
Can you give me a code example of this?
Thank you.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You need to take care any thing, you will get the error number and message automatically, to check that try to get the file in.html from the url you specified. You will get a html with title "404 Not found" and
error message "The requested URL /in.html was not found on this server".


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rzvika3Author Commented:
Do you mean that i will get automatically an html error page?
If so, how can i know that there was an error? according to exception?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.