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 7697779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:05:24+00:00 2026-05-31T22:05:24+00:00

I am defining a url in processing (or java) with the line: URL base

  • 0

I am defining a url in processing (or java) with the line:

URL base = new URL("http://www.google.com/");

So I get the following error: Default constructor cannot handle exception type MalformedURLException thrown by implicit super constructor. Must define an explicit constructor. Which I assume is because if my url isn’t valid, there’s no corresponding catch to catch the error (because I’m declaring my url outside of try).

But, i want to define this url outside of try, because my main try block is in a loop and the url is static. So there’s no need to do the definition of the url more than once (and because it’s static i’m not afraid of any MalformedURLExceptions).

How do I do that? Is there really no other way than to define the url within a separate try block? (because that seems a bit too much for just a simple static url)


// import libraries
import java.net.*;
import java.io.*;

// url
URL base = new URL("http://www.google.com");

void setup() {
}

void draw() {
  try {
    // retrieve url
    String[] results = loadStrings(base);

    // print results
    println(results[0]);
  }  
  catch (MalformedURLException e) {
    e.printStackTrace();
  }
  catch (ConnectException e) {
    e.printStackTrace();
  }
  catch (IOException e) {
    e.printStackTrace();
  }

  // stop looping
  noLoop();

}
  • 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-05-31T22:05:25+00:00Added an answer on May 31, 2026 at 10:05 pm

    You just need to define a default constructor for your class which throws MalformedURLException:

    class MyClass {
        private URL url = new URL("http://www.google.com");
    
        public MyClass() throws MalformedURLException {}
    }
    

    The error is happening because the constructor Java is generating for your class isn’t able to throw anything, you must specify that it might.

    Edit

    Seeing your code, your other option is this (I assume setup() gets called before you use the URL)

    URL url;
    
    void setup() {
        try {
            url = new URL("http://www.google.com");
        } catch (MalformedURLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying the following tutorial http://www.androidsdkforum.com/android-sdk-development/3-oauth-twitter.html i am having trouble understanding callback URL my twitter
I'm defining a partner through a route based on the url e.g. my.domain.com/:partner/:controller/:action Now
I am currently defining regular expressions in order to capture parameters in a URL,
I'm defining a datagrid's RowDetailsTemplate in the following way: RowDetailsTemplate={StaticResource defaultTemplate} where <UserControl.Resources> <DataTemplate
I am currently defining the src attribute of my Iframe with the URL +
I'm defining the following variables: $rel_path = bloginfo('template_directory'); $thumb_directory = $rel_path/images/portfolio/; $orig_directory = $rel_path/images/portfolio/thumbs;
I want to be able to specify a base url for all my routes
When defining something like a background image URL in a CSS file, when using
What should be the servlet url-pattern for following URL Paths: INFO: Pre-instantiating singletons in
I'm trying to get the 301 URL Tracker package for Umbraco to work to

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.