Unreported exception java.lang.exception

后端 未结 2 531
野的像风
野的像风 2021-01-07 12:32

Unreported exception java.lang.exception : Must be caught or declared to be throw. Why this problem will occur? Is it some simple method that can help to solve this problems

相关标签:
2条回答
  • 2021-01-07 13:14

    Your encrypt() method throws an Exception. This means that where you're calling this method, you should explictly throw this Exception or handle it using a try-catch block.

    In your case, for this particular code:

    byte[] pass = encrypt(password);
    String pw = new String(pass);
    

    You should either enclose it in:

    try{
     byte[] pass = encrypt(password);
     String pw = new String(pass);
    }catch(Exception exe){
     //Your error handling code
    }
    

    or declare the method where this code is enclosed with throws Exception.

    • If you are new to exception handling, consider reading this: Lesson: Exceptions from the Java Tutorials

    • Also, here's another interesting read on "Guidelines on Exception propagation (in Java)"

    0 讨论(0)
  • 2021-01-07 13:14

    1. There are 2 ways to handle the exception.

     - Either `declare` it
     - or `Handle` it.
    

    2. encrypt() method above throws an Exception

    So either declare it on the method declaration in which you are calling it.

    eg:

    public void MyCallingMethod() throws Exception{
    
         byte[] pass = encrypt(password);
         String pw = new String(pass);
    
    
    }
    

    Or handle it with try/catch block, finally is optional

    try{
    
         byte[] pass = encrypt(password);
         String pw = new String(pass);
       }catch(Exception ex){
    
    
      }
    
    0 讨论(0)
提交回复
热议问题