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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:44:49+00:00 2026-06-13T18:44:49+00:00

I am having difficulty using junit 4’s expected annotation to look at exceptions. I

  • 0

I am having difficulty using junit 4’s expected annotation to look at exceptions. I can’t compile the code because there’s an unhandled exception.

Here’s a simple example that creates the situation:


import static org.junit.Assert.*;
import java.io.UnsupportedEncodingException;
import org.junit.Test;

public class Simple {
    @Test(expected=UnsupportedEncodingException.class)
    public void simpleTest(){
        String a = "";
        a.getBytes("UTF-123");
    }
}

I get a compilation error saying “Unhandled exception type UnsupportedEncodingException”

This makes sense, and I can fix this by declaring that simpleTest throws UnsupportedEncodingException but I’ve seen lots of examples online where people don’t do that (which would be nice, when writing lots of test cases).

Is there a way to configure the test case so that I don’t have to explicitly declare what exceptions will be thrown?

  • 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-13T18:44:51+00:00Added an answer on June 13, 2026 at 6:44 pm

    As far as I know, UnsupportedEncodingException is a checked exception. So, compiler would expect a throws clause for a checked exception. I guess your code will work, if say you tried with an unchecked exception like ArithmeticException.

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

Sidebar

Related Questions

I have some jQuery code that is having difficulty running because I'm using it
I am having difficulty downloading the Android source code using the recommended instructions, due
I am having difficulty in using Ctrl key on emacs. Because, fn key resides
I am using Python 2.6, and am having difficulty understanding why the following code
I'm having difficulty using data created with canvas's todataurl() method. Currently my code sends
I am having difficulty using JQuery UI Modal Dialog when submitting a form. The
I am having difficulty using a thirdparty library registration function to register a callback.
I'm having difficulty using MooseX::Declare properly when calling BUILDARGS. I'm trying to create an
Hi, I'm having difficulty using media wiki in a frame. I'm using the following
I am using Jquery Cookie but am having difficulty setting and retrieving the info.

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.