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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:46:58+00:00 2026-06-01T23:46:58+00:00

I am having trouble with throwing and catching exceptions. Here is the code for

  • 0

I am having trouble with throwing and catching exceptions.

Here is the code for assignSeat(), assignSeat is called in a try block in another class.

void assignSeat(String passengerName, int x, int y) throws SeatOccupiedException, InvalidPassengerNameException
{
    Seat tSeat = airplane.getSeat(x,y);
    if (tSeat!=null)
    {
        if (passengerName.isEmpty() || !passengerName.matches("[a-zA-Z]+"))
        {
            throw new InvalidPassengerNameException();
        }
//excluded else if
    else if (foundPassenger(passengerName))
        {
                    airplane.seatList.get(airplane.seatNumber(passengerName)).unOccupy();   
                    tSeat.occupy();
                    for (int i = 0; i<passengers.size();i++)
                        if (passengers.get(i).getName().equals(passengerName))
                            passengers.get(i).changeSeat(tSeat.getSeatName());
        }
        else if (!tSeat.occupied)
        {
            tSeat.occupy();
            addPassenger(passengerName, tSeat.getSeatName());
        }
        else if (tSeat.occupied)
        {
            throw new SeatOccupiedException();
        }
    }

and here is the code that calls assignSeat() and is in another class (I won’t copy the whole class to make it look clearer)

if (afComp.currentAF != null)
            {
                try
                {
                    afComp.currentAF.assignSeat(nameField.getText(), x, y); //<-Problem here, "Unhandled exception type SeatOccupiedException"
                }
                catch (SeatOccupiedException exception) //<-Problem here, "Unreachable catch block, This exception is never thrown from the try statement body"
                {

                }
                catch(InvalidPassengerNameException exception) //<-No problems.
                {

                }
            }

Whats wrong with the try block? why won’t it throw the SeatOccupiedException?

Exception classes:

SeatOccupied:

package a2;

public class SeatOccupiedException extends Exception
{
    public SeatOccupiedException(){}
}

InvalidPassengerName:

package a2;

public class InvalidPassengerNameException extends Exception
{
    public InvalidPassengerNameException() {}
}

Every class I have is in package a2

imports for class that calls assignSeat

package a2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.*;

imports for class that has assignSeat

package a2;
import java.util.ArrayList;
  • 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-01T23:47:00+00:00Added an answer on June 1, 2026 at 11:47 pm

    Most probably, you have two SeatOccupiedException classes in two different packages, your method throws the class from one package while your code catches the other class.

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

Sidebar

Related Questions

Fairly new to .Net here and having trouble with a line of code. I
I am having trouble with XMLTextWriter.WriteStartElement throwing an exception: System.InvalidOperationException when trying to write
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
I was having trouble debugging why a route was throwing a 404 when I
I'm having trouble with jQuery not recognising elements on my page and throwing an
I'm having trouble with a NotifyIcon in WPF, the second line is throwing an
I having trouble with app that uses my static libs. Here is my setup:
I am having some trouble getting this simple code to work: #pragma once #include
Having trouble getting the javascript alert to display from my code behind. c# -

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.