java - Custom thrown exception causes unexpected compilation error -


I wanted to create a custom exception to handle the possible partition with zero, but the compiler states that I declared exception Details are not given.
Sorry for this kind of question, but I am an initial and object-oriented programming in Java.

  public class exception_teaster {public static zero main (string arguments []) {Exception_Tester et = new exception_taster (); Int x1; Int x2; X1 = 5; X2 = 0; Et.printResults (x1, x2); } Zero print result (int a, int b) throws arithmetic expressions {System.out.println ("Add:" + (a + b)); System.out.println ("sub:" + (A-B)); System.out.println ("Basic:" + (A * B)); If (b! = 0) System.out.println ("div:" + (a / b)); else {exception myException = new arithmetic exception ("You can not divide by zero!"); Throwing my impression; }}}   


Compiler Error: unreported exception java.lang.Exception; Should be caught or declared

The problem is that you are throwing exceptions which is undeclared in your method:

  exception myException = new arithmetic exception ("You can not divide by zero!"); Throwing my impression; // Here you can throw exceptions   

You can resolve this by any of them:

  • Announce that your method Exception :

      zero printResults (int a, int b) exception {// ...}    
  • New Throw Arithmetic Exposure as is:

      // Exception myException = New arithmetic exception ("You can not divide by zero!"); Throw new arithmetic exception ("You can not divide by zero!");         

    Therefore, your code may be:

      zero print (int a, int b) {System.out.println ("add:" + (a + b)); System.out.println ("sub:" + (A-B)); System.out.println ("Basic:" + (A * B)); If (b! = 0) {System.out.println ("div:" + (a / b)); } And {new arithmetic exception ("You can not divide by zero!"); }}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -