Using try-catch java

前端 未结 4 1628
甜味超标
甜味超标 2020-12-07 03:08

So just got into the basics of try-catch statements in Java and I\'m still a bit confused on some differences within the syntax.

Here is the code I\'m trying to anal

4条回答
  •  日久生厌
    2020-12-07 03:43

    "throws" is a declaration that a method will throw certain exceptions. This is enforced by the java compiler for checked exceptions, and not for errors or unchecked exceptions.

    "throw new" are two keywords in java so we can break it down...

    • "throw" is an operator that throws an exception
    • "new" is an operator that creates a new instance of an object

    the "try" block allows you to execute methods that declare they throw exceptions, and that is where you use the "catch" clause, in order to catch those thrown exceptions.

    Additionally there is also the try-with-resources block where you can use a try block to operate on a consumable resource(say, a stream) that implements AutoCloseable, and then close it.

    There is also the "finally" clause to a try block, which allows you to execute cleanup or any other methods that MUST execute after a try block, regardless of whether exceptions occur or not.

提交回复
热议问题