exception - Try Catch Block in Java -


Then I have given this code and to create an exception and then use to / hold Block to catch it. I have already made an exception, at the bottom of the code. But I have never used the try / hold block and I am not sure how to implement it

The exception is if the rank enum has been entered. I need to use toString with the exception of holding me, but I'm pretty sure I can understand it.

  package pracapp4; Import java.util.Scanner; Public Class Staff Staff {enum title} {DEPARTMENT_HEAD, DIRECTOR, DEAN, VICE_CHANCELLOR, CHANCELLOR} provides the title of private title; Public staff () {super (); Title = title. DEPARTMENT_HEAD; } Public employee (string first name, string last name, full salary, title title) {super (first name, last name, salary); This.title = Title; } @ Override public string toasting () {return super.toString () + "\ n \ t title:" + title; } @ Override Public Zero Exposure () {System.out.println ("<< Staff>>" + this); } @ Override public zero input (in scanner) {super.input (in); If (in.hasNext ()) {this.title = Enum.valueOf (title squared, in.next ()); }} Class invalidRankException expands exception {public invalid ranks exception () {super ("unknown rank name:"); }}}}  

You do not need that exception. When you add your title enum, the way you are in the staff constructure, it is impossible to provide the value, which is not in the value, you will never get an invalid title. This is the whole enum point.

Update: A small code review is an order here.

  1. Your default constructor is odd, can you be the head of the department without the name or salary? The call to "this" is appropriate here, and the better default values ​​are in order.
  2. Full numbers for salary - okay no unit? USD? Euro?
  3. Can the salary be negative? Does this make any sense? (Note to self: do not work there.)
  4. Why do you need both for string and display? What is the display override? I recommend the display display and paste it with the string.
  5. There is no point in your input method.
  6. Why is this exception an internal class?

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -