In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Catalogue
The thought of exception handling
Test environment preparation
Two ways of exception handling
Method 1: simple exception handler
Method 2: custom exception handlers
Summary
The thought of exception handling
Test environment preparation
First write a class in the DemoController control layer as the controller for test access
Package com.itheima.controller;import com.itheima.exception.MyException;import com.itheima.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import java.io.FileNotFoundException;@Controllerpublic class DemoController {@ Autowired private DemoService demoService @ RequestMapping (value= "/ show") public String show () throws FileNotFoundException, MyException {System.out.println ("show running"); demoService.show1 (); / / demoService.show2 (); / / demoService.show3 (); / / demoService.show4 (); / / demoService.show5 (); return "index";}}
Then write the interface DemoService and the implementation class DemoServiceImpl in service
Package com.itheima.service;import com.itheima.exception.MyException;import java.io.FileNotFoundException;public interface DemoService {public void show1 (); public void show2 (); public void show3 () throws FileNotFoundException;public void show4 (); public void show5 () throws MyException;} package com.itheima.service.impl;import com.itheima.exception.MyException;import com.itheima.service.DemoService;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream Public class DemoServiceImpl implements DemoService {public void show1 () {System.out.println ("type conversion exception"); Object str = "zhangsan"; Integer num = (Integer) str;} public void show2 () {System.out.println ("divide by zero exception"); int I = 1 / 0 } public void show3 () throws FileNotFoundException {System.out.println ("file cannot find exception"); InputStream in = new FileInputStream ("C:/xxx/xxx/xxx.txt");} public void show4 () {System.out.println ("null pointer exception"); String str = null; str.length () } public void show5 () throws MyException {System.out.println ("Custom exception"); throw new MyException ();}}
The MyException is a custom exception and is declared under another package of itheima. There is no implementation at this time:
Visit / show, because the show1 method called first will report a type conversion exception:
The environment is ready.
Two ways of exception handling
Method 1: simple exception handler
Method 1 is very simple, just configure the corresponding configuration file:
Then access it again, and you can see that you jump to the error view:
Method 2: custom exception handlers
Steps
1. Create an exception handler class to implement HandlerExceptionResolver
2. Configure exception handlers
3. Write the exception page
4. Test abnormal jump
Demo
Step 1: create an exception handler class to implement HandlerExceptionResolver
Package com.itheima.resolver;import com.itheima.exception.MyException;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse Public class MyExceptionResolver implements HandlerExceptionResolver {/ * this is the method parameter Exception: exception object return value ModelAndView: jump to error view information * / public ModelAndView resolveException (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {ModelAndView md = new ModelAndView () / * this is just a rough demonstration of code logic and will not be written like this in actual development. The following demonstration just tells us that we can judge exception information in this method * / if (e instanceof MyException) {md.addObject ("info", "custom exception") } else if (e instanceof ClassCastException) {md.addObject ("info", "type conversion exception");} md.setViewName ("error"); return md;}}
Step 2: configure the exception handler in the configuration file of SpringMVC
Just test the interview.
Summary
After reading the above, do you have any further understanding of the detailed explanation of how to handle Java SpringMVC exceptions? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.