java - Try - Catch Exception issue -
I am trying to create a desktop calculator with different options (hex, binary, octal, decimal) and if any The value is not recorded in that mode, an error saying, for example "BinaryInteger is expected, the user entered 90 9 382" and again indicated. The only error that is visible now due to my catch (exception e) is "invalid option". How do I write exceptions / errors for invalid inputs?
import java.util. *; Public class IntDriver {// declared areas Private Stable LongInteger Number 1; Private Stable LongInteger num2; Private static string over; Private static input mode = 0; // operators use private last fixed string [] operators = {"", "+", "-", "*", "/"}; Public static zero main (string [] args) {scanner KB = new scanner (System.in); Boolean run = true; While (run) {displayMenu (); While {true} {try {System.out.print ("option or value -> gt;"); String option = kb.nextLine (); Switch (option.toLowerCase ()) {// modes // decimal mode case "decimal": case "DCM": case "dec": mode = 0; break; // Binary Mode Case "Binary": Case "Bin": Mode = 1; break; // Octa Mode Case "Octak": Case "Act": Mode = 2; break; // hex mode case "hexadecimal": case "hex": mode = 3; break; // Case of quitting "q": Case "left": System.out.println ("Thank you.") Is a good day.); Run = false; break; // operator // join case "+": opr = "+"; break; // subtraction matter "-": opr = "-"; break; // multiply "*": opr = "*"; break; // split case "/": opr = "/"; break; // equivalent case "=": operated (); break; Default: LongIntergeer Temporary; // With modes in: switch (mode) {case 1: temp = new binary interser (option); break; Case 2: temp = new octal integer (option); break; Case 3: temp = new hexintengar (option); break; Default: temp = New DecInteger (option); break; } // If not num1 = LongInger is temporary (num1 == faucet) {num1 = temp; } Else {if (opr == null) {new unsupported exception exception (); } num2 = temp; } break; } break; } Hold (unsupported option exception e) {System.out.println ("Invalid option; operator not specified. \ N"); } // Invalid option entered. Grip (exception e) {System.out.println ("invalid choice. \ N"); }}}} // menu displayed private static zero display menu () {System.out.println (); Switch (Mode) {Case 1: System.out.println ("Binary Mode"); break; Case 2: System. Out.printLN ("octal mode"); break; Case 3: System.out.println ("hexadecimal mode"); break; Default: system.out.println ("decimal mode"); break; } If (num1! = Null) {System.out.print (num1 + "\ t"); } If (opr! = Null) {System.out.print (opr + "\ t"); } If (num2! = Null) {System.out.print (num2 + "\ n"); } System.out.println ("\ n \ n \ t mode: \ t \ t operator:"); System.out.println ("\ tBin - Binary \ t \ t +"); System.out.println ("\ tOct - Octa \ t \ t-"); System.out.println ("\ tDcm - Decimal \ t \ t *"); System.out.println ("\ thex - hexadecimal \ t /"); System.out.println ("\ tQ - Skip \ t \ t = \ n"); } Private static zero operation () throws exception {if (num1 == null || num2 == null || opr == null} {new exception ("not enough number."); } Switch (opr) {case "+": num1 = num1.calcValue (opr, num2, mode); break; Case "-": num1 = num1.calcValue (opr, num2, mode); break; Case "*": num1 = num1.calcValue (opr, num2, mode); break; Case "/": num1 = num1.calcValue (opr, num2, mode); break; Default: throw a new exception ("Invalid operator."); } Num2 = Faucet; Opr = ""; }}
You can increase more than 4x for each case:
Public Class YourCustomException Exception {// Copy Paste Extends and Modify what is relevant in your case. } .. and then throw those 4 exceptions when you have invalid input for your mode. This is inappropriate in your case, though. I think your case is quite simple that you can do just that:
public class DecInteger {public DecInteger (string option) {if (isNotDecimal (option) throw new exceptions ( "Input option" + option + "is not a decimal"); }} .. Then in your catch block you will need to do something like this:
hold (exception pre) {System.out.println ( Ex.getMessage); } Technically this is not a good practice to adopt the exception type exception , it is better to create a sub-type which is more for the actual exception Descriptive, but your case is very easy, where it really does not matter.
Comments
Post a Comment