cannot throw exception from switch-"unreachable statement"

Hi,

why is this an unreachable statement?

publicstatic Msg newMsg(int type, Object data)

{

Msg msg;

switch (type)

{

case MsgTypes.MSG_CLUSTER_CREATED:

msg=new MsgClusterCreated();break;

....

case MsgTypes.MSG_REPLY:

msg=new MsgReply();break;

default:

thrownew MessageTypeNotDeclaredException();break;//unreachable statement!!!

}

msg.setDataSegment(data);

return msg;

}

[1125 byte] By [uiga] at [2007-11-26 12:21:35]
# 1

When you throw an exception, the program flow never continues "normally" on the next line control is always transferred to the nearest exception handler. Thus control never reaches the break statement on the same line, and "unreachable statements" like this are illegal in Java.

Solution: remove the unnecessary "break;"

jsalonena at 2007-7-7 15:13:32 > top of Java-index,Archived Forums,Socket Programming...