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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:57:51+00:00 2026-06-09T17:57:51+00:00

Noob Question. Stuck on the following code. Getting Default constructor cannot handle exception type

  • 0

Noob Question.

Stuck on the following code. Getting “Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor” error for the line in bold

private FileWriter fileWriter = new FileWriter(file);
To be specific, my question is …..how do i create a explicit constructor for filewriter?

2nd issue: i know method appendtoLog is incorrect. I only want this method to do bufferWriter.write(logInfo) but for that i need to call the bufferWriter object already created.But as you can see, i have instantiated it in another method which prevents it from being available to appendtolog method. Pls suggest solutions or mistake in my approach.

Any help?
Thanks.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

 public class Logging {


private DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
private Date date = new Date();
private File file = new File("c:\\view\\" + dateFormat.format(date) + "\\"
        +"\\SmokesLog.txt");
private FileWriter fileWriter = **new FileWriter(file);**

public void createLogFile() throws Exception {
try{
if(!file.exists()){
    file.createNewFile();
    System.out.println("file name is "+ file.getName());
      BufferedWriter bufferWriter = new BufferedWriter(new     
  FileWriter(file.getName(),true));
       bufferWriter.write("Log Started for Test");
}
    } catch (IOException e) {
            System.out.println("code failed in creating logfile");
    }

}

public void appendToLog(String logInfo) throws IOException {
    System.out.println("code got to appendToLog method");
    // below does not append.need to find better method.
    if (file.exists()) {
        BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
        bufferWriter.write(logInfo);
        System.out.println("Done");
    }
}
}
  • 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-09T17:57:52+00:00Added an answer on June 9, 2026 at 5:57 pm

    As people have said, it’s because creating a FileWriter throws an exception. While i do recommend using a Logger package instead, here’s a way you can overcome this:

    Use an explicit constructor, catch the exception, and most likely rethrow it wrapped in a RuntimeException.

    private FileWriter fileWriter;
    
    public Logging() {
        super();
        try {
           fileWriter = new FileWriter(file);
        }
        catch (IOException e) {
           throw new RuntimeException(e);
        }
    }
    

    OR use an instance initializer block, which executes after any call to super() in your constructor, but before any other code in your constructor. In your case, if you don’t have a constructor, an implicit super() is called, which is OK, because you’re extending Object. So the initializer block executes immediately after that. So the following is pretty much functionally equivalent to the above code.

    private FileWriter fileWriter;
    {
        try {
           fileWriter = new FileWriter(file);
        }
        catch (IOException e) {
           throw new RuntimeException(e);
        }
    }
    

    As i said, i don’t recommend doing it this way though, and a real logging package (slf4j + log4j) would be more appropriate.

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

Sidebar

Related Questions

Java noob question: Consider the following C array and initializer code: struct { int
Noob question: Consider the following C# code: public IEnumerable<xpto> CalculatedList { get { foreach(var
a noob question here. I have the following code: - (IBAction)selectExistingPicture { if ([UIImagePickerController
noob question: I'm building a checkout system for an online commerce site. I'm getting
Very noob question, I know, but I didn't manage to handle real-time slider move
A noob question. I'm putting together my first database and have the following design
Absolute noob question. I have a form <form> <input type=text id=cars name=cars/> </form> When
Hi there this might me a very noob question but I'm stuck, and I'm
Noob question here. Please see code below. Where can I read more on this?
I have a noob question here. I'm getting my head around the C++ structure

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.